基于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. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

  2. Ionic2 window开发环境搭建

    1.软件安装 Node.jsCordova & Ionic CLIJava SDK (一定要安装jre1.8)Android SDK 可根据链接http://www.jianshu.com/p ...

  3. WIN7下使用sublime text3替代arduino IDE(安装方法和所遇到的问题)

    用了一段时间Arduino IDE,感觉比较简陋~~很多功能都没有~虽然不影响使用啦~(主要是启动速度有点慢...我的破笔记本….), 网上搜寻了下,发现sublime text有插件可以替代,这就比 ...

  4. Nginx 的安装配置入门(mac)

    1.安装Nginx服务器: 执行命令 brew install nginx 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/etc/nginx/nginx.conf (配置 ...

  5. HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)

    6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...

  6. Codeforces 538 B. Quasi Binary

    B. Quasi Binary   time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. 线段树+Dfs序【CF620E】New Year Tree

    Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]. 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种 ...

  8. 学习LSM(Linux security module)之一:解读yama

    最近打算写一个基于LSM的安全模块,发现国内现有的资料极少.因此打算自己琢磨一下.大致的学习路线如下: 由易至难使用并阅读两到三个安全模块->参照阅读模块自己实现一个安全模块->在自己实现 ...

  9. 第1天-html快速入门

    开发工具:HBuilder 创建项目: 在电脑本地磁盘创建项目目录,如"D:\project" 打开HBuilder,这个工具默认会创建一个项目,我们删掉即可,然后新建项目:&qu ...

  10. Mysql数据库的安装及配置

    本文转载自http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得搭建 ...