Struts2 * 关于解决登录的问题
作者:南城琉璃 发布时间:2023-07-02 14:06:09
* 的工作原理如图 * 是由每一个action请求(request)都包装在一系列的 * 的内部,通过redirectAction再一次发送请求。
* 可以在Action执行直线做相似的操作也可以在Action执行直后做回收操作。
我们可以让每一个Action既可以将操作转交给下面的 * ,Action也可以直接退出操作返回客户既定的画面。
接下来我们该如何定义一个 * :
自定义一个 * 如下:
1、实现Interceptor接口或者继承AbstractInterceptor抽象类。
2、创建一个Struts.xml文件进行定义 * 。
3、在需要使用的Action中引用上述定义的 * ,为了方便也可将 * 定义为默认的 * (<default-interceptor-ref name="myStack"/>),
这样在不加特殊声明的情况下所有的Action都被这个 * 拦截<param name="excludeMethods">loginView,login</param>。
①Interceptor接口声明三个方法:
public class LoginInterceptor implements Interceptor {
private Map<String,Object> session = null;
public void destroy() { }
public void init() { }
public String intercept(ActionInvocation actionInvocation) throws Exception {
8Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你访问的Action是:"+myAction);
}
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
注:该方法可以不加:<param name="excludeMethods">loginView,login</param>
②让它继承 MethodFilterInterceptor:
public class LoginInterceptor extends MethodFilterInterceptor {
private Map<String,Object> session = null;
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
/*
Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你访问的Action是:"+myAction);
}
*/
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
}
③UserAction继承ActionSupport 实现 ModelDriven<User>和SessionAware:
public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{
private Map<String,Object> session = null;
private User user = null;
//驱动模型
public User getModel() {
this.user = new User();
return this.user;
}
public void setSession(Map<String, Object> map) {
this.session = map;
}
public String loginView(){
return "loginViewSuccess";
}
public String login(){
if ("admin".equals(user.getUserName())&&"123456".equals(user.getUserPassword())){
session.put("user",user);
return this.SUCCESS;
}else{
return this.ERROR;
}
}
}
Struts.xml文件中:
<struts>
<package name="myPackage" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!--excludeMethods需要生效的话,自定义的 * ,不能使用实现Interceptor接口,而是extends MethodFilterInterceptor-->
<param name="excludeMethods">loginView,login</param><!--不用此行时 我们可以配合①使用 * -->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--配置一个默认 * ,也就是所有的Action都必须使用-->
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results>
<!--不写method,默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction" method="execute">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<!--
<interceptor-ref name="myStack"></interceptor-ref>
-->
<!--注释这里也可以放该代码 只不过每一个action都要放比较麻烦
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
<action name="otherFunctionAction" class="com.nf.action.OtherFunctionAction">
<!--不写name,默认就是success-->
<result>/WEB-INF/jsp/otherFunction.jsp</result>
</action>
<action name="userAction_*" class="com.nf.action.UserAction" method="{1}">
<result name="loginViewSuccess">/WEB-INF/jsp/loginView.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="success" type="redirectAction">indexAction</result>
<allowed-methods>login,loginView</allowed-methods>
</action>
</package>
</struts>
其中,<param name="excludeMethods">loginView,login</param> 配置的过滤方法,意思是 * 对其中的方法不起作用。在我这里,loginView是跳转到登录页面的方法。
login 是验证用户名和密码的方法,在其中会将通过验证的用户名放入session中。
总结:
1.在struts2 中,所有的 * 都会继承 Interceptor 这个接口。
2.如果我们没有添加 * ,struts2 会为我们添加默认 * 。当然我们要是指定了 * ,我们自己的 * 就会取代默认的 * ,
那么我们就不能享受默认 * 提供的一些功能。所以,一般我会把默认 * 也加上。
例如,在以上配置项中,action 里面再加上<interceptor-ref name="defaultStack"></interceptor-ref>
来源:http://www.cnblogs.com/wangpengpeng/archive/2017/10/10/7647599.html
猜你喜欢
- 环境配置Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclis
- java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java n
- JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其
- 这篇文章主要介绍了SpringBoot跨域Access-Control-Allow-Origin实现解析,文中通过示例代码介绍的非常详细,对
- EventLoopGroup介绍在前面一篇文章中提到了,EventLoopGroup主要负责2个事情,这里再重复下:它主要包含2个方面的功能
- eclipse中的javac命令与java命令一、eclipse的javac命令:当eclipse对.java(源文件)文件进行保存操作时(
- 前言xxljob 是采用 java 开发的开源的任务调度系统,架构上分为调度管理器、执行器,目前除了官方提供的 java 执行器外,也有 g
- 第 1 步:将这个 Spring Boot 项目的打包方式设置为 war。<packaging>war</packagin
- 序言小编在项目中有遇到使用 flutter 实现扫码枪接入的需求。为方便使用,小编把能力封装成 package 并发布。好记性不如烂笔头,下
- 今天没有继续学习新知识点,画出了一个随鼠标画出的 图形,知识点全都是之前学的,话不多说,先上图:代码部分: // ope
- 前言痛点:在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环境对项目的需求也不相同。在此之前,
- ssm mybatis配置多个mapper目录通配符配置比如目录的结构如下:com/test/web/user/mappercom/test
- 1. 前言前面的关于 Spring Security 相关的文章只是一个预热。为了接下来更好的实战,如果你错过了请从 Spring Secu
- 二叉搜索树的定义它是一颗二叉树任一节点的左子树上的所有节点的值一定小于该节点的值任一节点的右子树上的所有节点的值一定大于该节点的值特点: 二
- try { // 方
- 本文实例为大家分享了Javaweb统计在线人数示的具体代码,供大家参考,具体内容如下1. 实现功能统计在线人数显示每个人的sessionId
- Spring @Order进行排序直接上代码public class OrderAnnotationTest { &
- 前言之前在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务
- 1、什么是FeignFeign 是 Spring Cloud Netflix 组件中的一个轻量级 RESTful 的 HTTP 服务客户端,
- java掩码 private static String nameMask(String name) throws Exception {