购物商城shopping连载(4)

本文详细介绍了用户登录流程,包括邮箱激活、登录页面设计、验证用户身份及退出机制。同时,实现了验证码功能,通过JSP和Struts2框架,确保用户输入正确验证码才能登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模块1:用户登录

用户在注册完成之后,需要登录。但是成功登录的前提是邮箱激活码激活成功。

登录页面

点击登录超链接,跳转到登录页面

<a href="${ pageContext.request.contextPath }/user_loginUI.do">登录</a>

UserAction loginUI方法

// 用户点击“登录”跳转到登录界面
    public String loginUI() {
        return "loginUI";
    }

struts2配置文件

这里写图片描述

看一下登录界面:

这里写图片描述

这里就不写前台校验了,校验法则和注册时一样。
填写用户名和密码之后,点击登录,访问url:

<form id="loginForm" method="post"  action="${ pageContext.request.contextPath }/user_login.do">

UserAction login 方法

// 用户登录
    public String login() {
        System.out.println(model.getUsername() + "," + model.getPassword());
        User user = userService.login(model.getUsername(), model.getPassword());
        if (user != null) {
            // 用户存在,把用户保存到session中
            ActionContext.getContext().getSession().put("user", user);
            // 跳转到首页
            return "loginSuccess";
        } else {
            addActionMessage("用户不存在或者用户未激活");
            // 跳转到登录页面
            return "loginUI";
        }
    }

由上可知,userservice调用了login方法,所有我们查看一下service层的login方法

service:

    /***
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    User login(String username, String password);

serviceImpl:

@Override
    public User login(String username, String password) {
         User user = (User)  sessionFactory.getCurrentSession()
        .createQuery("from User where username=? and password=? and state=1")
        .setParameter(0, username)
        .setParameter(1, password)
        .uniqueResult();
         if (user != null) {
                return user;
            }
        return null;
    }

再回到UserAction的login方法,判断,如果user不为空,则把user对象保存到session中,并重定向到商城首页。
那么问题来了,怎么跨action重定向呢?
在配置文件中这样写:

    <!-- 登录成功后重定向到首页 -->
        <result name="loginSuccess" type="redirectAction">
            <param name="actionName">index_shopping</param>
        </result>

在上面的struts2配置文件的图片中也可以找到。
ok,登录成功之后,前台的页面显示(部分):

这里写图片描述

看,用户名是小公举。
到此,我们把退出功能也一并做了,退出就是把用户从session中移除就行了。
退出的超链接:

<a href="${ pageContext.request.contextPath }/user_quit.do">退出</a>|

UserAction quit 方法

    //用户退出
    public String quit(){
        //把用户从session中移除
        ActionContext.getContext().getSession().remove("user");
        // 跳转到登录页面
        return "loginUI";
    }

退出之后跳转到登录页面。这一步也成功完成了。

模块2:验证码

JSP:

<tr>
                                    <th>验证码:</th>
                                    <td><span class="fieldSet"> <input type="text"
                                            id="captcha" name="captcha" class="text captcha"
                                            maxlength="4" />
                                            <img id="vCode" class="captchaImage"
                                            src="${ pageContext.request.contextPath }/vCode_yanzhengma.do"
                                            title="点击更换验证码" /> </span></td>
                                </tr>

由代码可知image标签的src访问路径是:

src="${ pageContext.request.contextPath }/vCode_yanzhengma.do"

Struts2配置文件:

<!-- 验证码 -->
        <action name="vCode_*" method="{1}" class="verificationAction"></action>

VerificationAction :

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class VerificationAction extends ActionSupport {

    public void yanzhengma(){
        VerificationCodeUtil.makeVerificationCode();
    }

}

VerificationCodeUtil.makeVerificationCode(); 是调用的一个工具类

这个工具类是来生成验证码的,详情见《制作验证码 》
给个截图:

这里写图片描述

到这里验证码成功显示出来了,但是怎么 更换验证码呢?
这里用jquery,点击验证码切换

jquery:

<script type="text/javascript" src="${ pageContext.request.contextPath }/js/jquery-1.3.2.js"></script>

js:

<script type="text/javascript">
    $(function(){
        $("#vCode").click(function(){
            $("#vCode").attr("src","${ pageContext.request.contextPath }/vCode_yanzhengma.do?time="+new Date().getTime());
        });

    });
</script>

因为频繁点击验证码,容易产生缓存,所以加一个时间戳来抵消缓存。
ok,现在点击图片就可以自由切换验证码了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值