基于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;width: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>

最新文章

  1. 【转载】Shell判断字符串包含关系的几种方法
  2. spark编写word count
  3. linux下设置进程优先级方法!
  4. Protocol Buffers(Protobuf)开发者指南---概览
  5. [翻译]Kafka Streams简介: 让流处理变得更简单
  6. FreeModbus Slave 改进的eMbPoll()【worldsing 笔记】
  7. JS1-属性操作
  8. enode框架step by step之框架要实现的目标的分析思路剖析1
  9. OS作业模拟进程分配与回收
  10. javascript中的Date对象
  11. ORA-600 [Kcbz_check_objd_typ_1] Running a Job (Doc ID 785899.1)
  12. php后台操作以及一些减缓服务器压力的问题
  13. 【刷题】BZOJ 4000 [TJOI2015]棋盘
  14. Eric6启动时“无法定位序数4540于动态链接库LIBEAY32.dll”的错误
  15. 【译】Serverless架构 - 3
  16. WPF的布局--StackPanel
  17. (数据科学学习手札37)ggplot2基本绘图语法介绍
  18. enum don&#39;t allocate any memory
  19. WCF服务测试工具
  20. 【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集

热门文章

  1. AC日记——斐波那契数列(升级版) 洛谷 P2626
  2. 清理tomcat缓存
  3. owasp zap 安全审计工具 功能详解
  4. xampp 通过pear安装PHP_UML
  5. Manacher【SP7586】NUMOFPAL - Number of Palindromes
  6. POJ 1981 Circle and Points (扫描线)
  7. 【二分图判定】hdu3478 Catch
  8. 十. 图形界面(GUI)设计6.文本框和文本区
  9. Spring IOC 中三种注入方式
  10. iOS消息传递机制