用户登录功能以及登录拦截
一、用户登录
思路:jsp实现表单提交,action用模型驱动的方式接收数据,在数据库查找有没有与之匹配的记录,若没有,回到登录界面(并将用户填的数据回显,并给出错误提示),若有,进入首页,显示用户名
1、jsp页面
<form action="${pageContext.request.contextPath }/loginAction_login.action" method="post">账号:<input type="text" name="account" value="${user.user_name }" /><br>密码:<input type="password" name="password" /><br><input type="submit" value="登录">
</form>
2、action
public class LoginAction extends ActionSupport implements ModelDriven<User> {private User user = new User();UserService service = new UserService();public String login() {List<User> list = service.login(user);if (list.size()==0) {this.addActionError("您输入的信息有误!");ServletActionContext.getRequest().getSession().setAttribute("user", user);return LOGIN;}else {ServletActionContext.getRequest().getSession().setAttribute("user", list.get(0));return SUCCESS;}}@Overridepublic User getModel() {return user;}}
3、配置struts2.xml
<package name="package1" extends="struts-default" namespace="/"><action name="loginAction_*" class="com.wasion.web.action.LoginAction" method="{1}"><result name="success" type="redirect">/index.jsp</result><result name="login">/login.jsp</result></action>
</package>
4、service及dao层实现
public class UserService {UserDao dao = new UserDao();public List<User> login(User user) {return dao.login(user);}}public List<User> login(User user) {Session session = HibernateUtils.openSession();Transaction ts = session.beginTransaction();Query query = session.createQuery("from User where user_name=? and user_password=? and user_code=?");query.setParameter(0, user.getUser_name());query.setParameter(1, user.getUser_password());query.setParameter(2, user.getUser_code());List<User> list = query.list();ts.commit();session.close();return list;
}
效果:
失败>>
成功>>
二、登录拦截
1、编写拦截器(继承MethodFilterInterceptor类)
public class LoginInterceptor extends MethodFilterInterceptor {@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");if (user == null) {
// 未登录ActionSupport actionSupport = (ActionSupport) invocation.getAction();actionSupport.addActionError("您没有权限,请先登录!");return actionSupport.LOGIN;}else {
// 已经登录return invocation.invoke();}}}
2、添加配置文件
<package name="package1" extends="struts-default" namespace="/"><!-- 拦截器声明 --><interceptors><interceptor name="LoginInterceptor" class="com.wasion.web.interceptor.LoginInterceptor"></interceptor></interceptors><global-results><result name="login">/login.jsp</result></global-results><action name="customerAction_*" class="com.wasion.web.action.CustomerAction" method="{1}"><result name="success">/success.jsp</result><result name="find_success">/jsp/customer/list.jsp</result><result name="findIdSuccess">/jsp/customer/edit.jsp</result><!-- 配置拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="LoginInterceptor"></interceptor-ref></action><action name="loginAction_*" class="com.wasion.web.action.LoginAction" method="{1}"><result name="success" type="redirect">/index.jsp</result><!-- 配置拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="LoginInterceptor"><param name="excludeMethods">login</param></interceptor-ref></action>
</package>
有一个问题,当我们在这个页面点击登录后,会产生页面嵌套的效果,只要在login.jsp表单提交的form中修改一个属性即可
<form action="${pageContext.request.contextPath }/loginAction_login.action" method="post" target="_parent">
用户登录功能以及登录拦截相关推荐
- 登录form php一个页面跳转页面,Extjs4中表单登录功能、登录成功跳转页面的代码...
本节内容: Extjs4实现的表单登录功能,登录成功跳转页面. 例子: 复制代码 代码示例: Ext.onReady(function(){ Ext.QuickTips.init(); Ext.cre ...
- Django框架项目——BBS项目介绍、表设计、表创建同步、注册、登录功能、登录功能、首页搭建、admin、头像、图片防盗、个人站点、侧边栏筛选、文章的详情页、点赞点踩、评论、后台管理、添加文章、头像
文章目录 1 BBS项目介绍.表设计 项目开发流程 表设计 2 表创建同步.注册.登录功能 数据库表创建及同步 注册功能 登陆功能 3 登录功能.首页搭建.admin.头像.图片防盗.个人站点.侧边栏 ...
- 记住密码 和 自动登录功能(登录)
前台页面: <body><form id="Form1" runat="server"><input type="hid ...
- CRM客户关系管理系统开发第二讲——实现用户的注册和登录功能
实现用户注册功能 创建用户表 首先创建一个数据库(例如crm),并在该数据库下新建一张用户表,笔者这里使用的数据库是MySQL. create database crm; use crm;CREATE ...
- 黑马点评项目-短信登录功能
一.导入黑马点评项目 1.代码下载 视频资源链接:P25 实战篇-02.短信登录-导入黑马点评项目 代码可以直接去黑马微信公众号上搜索,或者从下面的网盘链接中下载:链接: https://pan.ba ...
- 如何使用Bootstrap Modal和jQuery AJAX创建登录功能
by Yogi 由瑜伽士 Bootstrap Modal is an excellent way to create a Login form on your website. In this tut ...
- 汽车租赁系统(2)-完成登录功能
文章目录 完成汽车租赁系统的登录功能 分析登录功能: 创建数据库的表 用户表(sys_users) 创建首页index.jsp 创建实体类 创建UserVo 创建Mapper 创建Mapper.xml ...
- uni-app前端开发(一)登录功能
登录功能 1.登录表单 <view class="bg"><!-- 登录logo --><u-image width="150rpx&quo ...
- Vue后台管理系统项目——实现登录功能
文章目录 登录功能 1. 登录业务流程 2. 登录业务相关技术点 3. 登录-- token 原理分析 4. 登录功能的实现(都记牢点) 登录页面的布局 创建登录组件 登录组件页面布局 登录组件头部布 ...
最新文章
- springmvc跨域(转)
- Fiddle抓包Https
- 网络实验环境搭建--1.工具及搭建思路
- 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
- QuerWrapper常用方法
- HDU2040 亲和数【水题】
- 他对我有成见,怎么办
- 一个素数,如果将其反转后仍然是一个素数,我们称这样的素数为反转素数。 例如13是一个素数,反转之后得到的31也是一个素数,那么13和31都是反转素数。
- 锐捷校园网环境下使用虚拟机上网
- 第五章软件项目风险管理
- 用JAVA实现蓝桥杯基础训练之特殊回文数:123321是一个非常特殊的数,它从左边读和从右边读是一样的,输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
- oppo计算机找不到,oppo手机照片在电脑显示不出来怎么办啊
- 从实践角度重新理解BIO和NIO
- A/B【费马小定理】
- 唱响艾泽拉斯-泰兰德的拥抱专辑
- 机房动力环境监测解决方案
- 外卖小程序发布商品时,要注意的地方-微信小程序教程32
- 女生句子简单干净霸气,适合女生霸气又很拽的句子
- 在R语言中进行局部多项式回归拟合(LOESS)
- Java——IDEA
热门文章
- 【Minecraft java edition 模组开发】(二):通过对岩浆怪和雪傀儡的源码分析,自己制作一个雪球怪
- linux下scp的常见问题解决方法
- html怎么做成正方形符号,javascript – Highcharts:使图例符号为正方形或矩形
- 爱奇艺内容中台数据中心的设计与实现
- 计算机鼠标不出现在电脑屏幕上,电脑一打开就这样了,台式的,电脑屏幕不出现鼠标,求大神帮助...
- ★【树状数组】【NOI2008】糖果雨
- 测试高薪必备:3步教测试人员破解子查询
- 生活-四种游泳姿势的动画图解
- 2017CCCC天梯赛决赛 赛后总结
- 使用LVS构建可伸缩WEB集群