基于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>

SSH——基于BaseDao和BaseAction实现用户登录的更多相关文章

  1. ssh框架的小实例(用户登录)

    刚学SSH框架写一个小实例,以便以后查看: 本案例简单的实现一个用户登录: 数据库方面就不写了,自己领悟吧!哈哈(根据user.hbm.xml文件就知道了) 我们一般可以创建下面几个包,什么意思呢,自 ...

  2. 基于Redis位图实现系统用户登录统计

    项目需求,试着写了一个简单登录统计,基本功能都实现了,日志数据量小.具体性能没有进行测试~ 记录下开发过程与代码,留着以后改进! 1. 需求 1. 实现记录用户哪天进行了登录,每天只记录是否登录过,重 ...

  3. 基于Servlet的MVC模式用户登录实例

    关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...

  4. Java SSH框架系列:用户登录模块的设计与实现思路

    1.简介用户登录模块,指的是根据用户输入的用户名和密码,对用户的身份进行验证等.如果用户没有登录,用户就无法访问其他的一些jsp页面,甚至是action都不能访问.二.简单设计及实现本程序是基于Jav ...

  5. 解决root用户ssh配置无密码登陆/hadoop用户照仿可以实现相同功能:hadoop用户登录并且把命令的所有root换成home/hadoop

    http://inuyasha1027.blog.51cto.com/4003695/1132896/ 主机ip:192.168.163.100(hostname: node0) ssh无密码登陆的远 ...

  6. linux下怎么查看ssh的用户登录日志

    linux下登录日志在下面的目录里: cd /var/log 查看ssh用户的登录日志: less secure linux日志管理: 1. 日志简介 日志对于安全来说,非常重要,他记录了系统每天发生 ...

  7. linux查看ssh用户登录日志与操作日志

    linux查看ssh用户登录日志与操作日志 2013-11-01转载   ssh用户登录日志 linux下登录日志在下面的目录里:  代码如下 复制代码 cd /var/log 查看ssh用户的登录日 ...

  8. 基于jwt的用户登录认证

    最近在app的开发过程中,做了一个基于token的用户登录认证,使用vue+node+mongoDB进行的开发,前来总结一下. token认证流程: 1:用户输入用户名和密码,进行登录操作,发送登录信 ...

  9. 【SSH学习笔记】用Struts2实现简单的用户登录

    准备阶段 在使用学习Struts2的时候首先要下载相应的架包 Struts2资源下载 这里建议下载第一个,在struts-2.5.14.1-all.zip里有很多实用的东西,不仅有架包还有官方为开发者 ...

随机推荐

  1. PHP开发经常遇到的几个错误

    错误1:foreach循环后留下悬挂指针 在foreach循环中,如果我们需要更改迭代的元素或是为了提高效率,运用引用是一个好办法: $arr = array(1,2,3,4); foreach($a ...

  2. ASP.NET中怎样将页面设为首页,加入收藏

    1.文字js脚本事件:<span onClick="var strHref=window.location.href;this.style.behavior=’url(#default ...

  3. Windows下python的第三方库的安装

    D:\Python27\Scripts\pip.exe install beautifulsoup4

  4. 51nod 1283 最小周长【注意开根号】

    1283 最小周长 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 一个矩形的面积为S,已知该矩形的边长都是整数,求所有 ...

  5. Linux命令之vim(二)

    这一章主要介绍vim编辑器的内部使用方法和注意事项 vim编辑器有四种工作模式:正常模式.插入模式.命令模式.可视模式.简单的判断方法就是看底部,什么都没有就是正常模式,光标在编辑器最底下时则是命令模 ...

  6. Encode and Decode Strings -- LeetCode

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. [Contest20180415]看无可看

    题意:有一个数列$f$,对$\forall i\geq2,f_i=2f_{i-1}+3f_{i-2}$,给定$f_0,f_1$,再给定一个集合$S=\{a_{1\cdots n}\}$和$k$,求$\ ...

  8. 【最大权闭合子图/最小割】BZOJ3438-小M的作物【待填】

    [题目大意] 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1...n编号),现在,第i种作物种植在A中种植 ...

  9. [POI2018]Prawnicy

    题目大意: 有$n(n\le10^6)$个线段,每个线段覆盖的范围是$[l_i,r_i]$,要求从中选取$k(k\le10^6)$个线段使得这些线段覆盖范围的交集最大,求最大交集及任意一种方案. 思路 ...

  10. map写数据到本地磁盘过程解析----spill和merge

    如上次分析,其实map函数中的context.write()调用过程如下所示: 梳理下调用过程,context的write方法其实是调用了TaskInputOutputContext类的write方法 ...