zoukankan      html  css  js  c++  java
  • SSH——基于BaseDao和BaseAction实现用户登录

                          

                  


    基于BaseDao和BaseAction实现用户登录

     1. 首先修改login.jsp页面,点击登录按钮,提交表单

     <a onclick="document.forms[0].submit()" id="loginform:j_id19" name="loginform:j_id19">
        <span id="loginform:loginBtn" class="btn btn-login" 
    style
    ="margin-top:-36px;">登录</span>
    </a>

    2. 在UserAction中提供login方法

    @Resource
        private IUserService userService;
    
        // 通过属性驱动接收验证码
        private String checkcode;
    
        public void setCheckcode(String checkcode) {
            this.checkcode = checkcode;
        }
        
        /**
         * 用户登录
         */
        public String login(){
            ValueStack valueStack =ActionContext.getContext().getValueStack();
            //生成的验证码
            String key =(String) ServletActionContext.getRequest().getSession().getAttribute("key");
            
            //判断输入的验证码是否正确
            if(StringUtils.isNotBlank(checkcode)&&checkcode.equals(key)){
                //验证码正确
                User user =userService.login(model);
                if(user!=null){
                    //登录成功,将User放入session域,跳转到系统首页
                    ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
                    return "home";
                }else{
                    //登录失败,设置错误提示信息,跳转到登录页面
                    this.addActionError(this.getText("loginError"));
                    return "login";
                }
            }else{
                //验证码错误,设置错误提示信息,跳转到登录页面
                this.addActionError(this.getText("validateCodeError"));
                return "login";
            }
        }

    3.  提供UserService

    //注入dao
            @Autowired
            private IUserDao userDao;
    
            @Override
            public User login(User model) {
                String username=model.getUsername();
                String password=model.getPassword(); //明文
                password=MD5Utils.md5(password);   //MD5加密
                return userDao.findByUsernameAndPassword(username, password);
                
                
            }

    4. 在UserDao中扩展方法,根据用户名和密码查询用户

        @Override
        public User findByUsernameAndPassword(String username, String password) {
    
            String hql="FROM User u WHERE u.username = ? AND u.password = ?";
            List<User> list=this.getHibernateTemplate().find(hql, username,password);
            if(list != null && list.size()>0){
                return list.get(0);
            }
            return null;
           
        }

    5. 在struts.xml中注册国际化文件,能够在登录失败时按照UserAction中的login方法提示错误信息

     <!-- 注册国际化文件 -->
        <constant name="struts.custom.i18n.resources" value="message" />

     login.jsp页面中使用struts2提供的标签展示错误提示信息

           

     login表单:

               <form id="loginform" name="loginform" method="post" class="niceform"
                        action="${pageContext.request.contextPath }/userAction_login.action">
                        <div id="idInputLine" class="loginFormIpt showPlaceholder"
                            style="margin-top: 5px;">
                            <input id="loginform:idInput" type="text" name="username"
                                class="loginFormTdIpt" maxlength="50" />
                            <label for="idInput" class="placeholder" id="idPlaceholder">帐号:</label>
                        </div>
                        <div class="forgetPwdLine"></div>
                        <div id="pwdInputLine" class="loginFormIpt showPlaceholder">
                            <input id="loginform:pwdInput" class="loginFormTdIpt" type="password"
                                name="password" value="" />
                            <label for="pwdInput" class="placeholder" id="pwdPlaceholder">密码:</label>
                        </div>
                        <div class="loginFormIpt loginFormIptWiotTh"
                            style="margin-top:58px;">
                            <div id="codeInputLine" class="loginFormIpt showPlaceholder"
                                style="margin-left:0px;margin-top:-40px;50px;">
                                <input id="loginform:codeInput" class="loginFormTdIpt" type="text"
                                    name="checkcode" title="请输入验证码" />
                                <img id="loginform:vCode" src="${pageContext.request.contextPath }/validatecode.jsp"
                                    onclick="javascript:document.getElementById('loginform:vCode').src='${pageContext.request.contextPath }/validatecode.jsp?'+Math.random();" />
                            </div>
                            <a onclick="document.forms[0].submit()" id="loginform:j_id19" name="loginform:j_id19">
                            <span
                                id="loginform:loginBtn" class="btn btn-login"
                                style="margin-top:-36px;">登录</span>
                            </a>
                        </div>
                        <div>
                            <font color="red">
                                <s:actionerror/>
                            </font>
                        </div>
                    </form>

     

  • 相关阅读:
    巧妙使用checkbox制作纯css动态导航栏
    前端资源多个产品整站一键打包&包版本管理(四)—— js&css文件文件打包并生成哈希后缀,自动写入路径、解决资源缓存问题。
    前端资源多个产品整站一键打包&包版本管理(三)—— gulp分流
    前端资源多个产品整站一键打包&包版本管理(二)——如何在bower的配置文件加上注释
    前端资源多个产品整站一键打包&包版本管理(一)
    Web 端唤起 App
    HTTP状态码
    集成一个好用的canvas签名板
    PHP处理字符中的emoji表情
    PHP集成微信支付(APP支付)
  • 原文地址:https://www.cnblogs.com/zjfjava/p/6961296.html
Copyright © 2011-2022 走看看