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