Struts2学习之拦截器栈
2024-10-20 09:22:34
© 版权声明:本文为博主原创文章,转载请注明出处
拦截器栈:
- 从结构上看:拦截器栈相当于多个拦截器的组合
- 从功能上看:拦截器栈也是拦截器
默认拦截器栈:
- 在struts-core.jar中的struts-default.xml中自定义了一个default拦截器栈,并且将其指定为默认拦截器栈
- 只要定义包的过程中继承struts-default包,那么默认defaultStack将是默认的拦截器
- 当为包中的某个action显示指定了某个拦截器,那么默认的拦截器将不会生效
- 拦截器栈中的各个拦截器的顺序很重要(一般讲默认拦截器器栈放在前面)
实例:
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.struts</groupId>
<artifactId>Struts2-AuthInterceptor</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.5.10</struts.version>
</properties> <dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<finalName>Struts2-AuthInterceptor</finalName>
</build> </project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true"> <!-- 首页 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <!-- Struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
4.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="login" method="post">
<font color="red">${loginError }</font><br/>
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
5.manager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理界面</title>
</head>
<body>
欢迎进入管理页面,该页面只有登录用户才能访问!
</body>
</html>
6.LoginAction.java
package org.struts.action; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements SessionAware { private static final long serialVersionUID = 1L; private String username;// 用户名
private String password;// 密码
private Map<String, Object> session;// Session @Override
public String execute() throws Exception { if ("admin".equals(username) && "123456".equals(password)) {
session.put("userInfo", username);
session.remove("loginError");
return SUCCESS;
} else {
session.put("loginError", "用户名或密码错误");
session.remove("userInfo");
return "login";
} } public void setSession(Map<String, Object> session) { this.session = session; } public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
7.AuthInterceptor.java
package org.struts.interceptor; import java.util.Map; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L; @Override
public String intercept(ActionInvocation invocation) throws Exception { // 获取session
ActionContext context = ActionContext.getContext();
Map<String, Object> session = context.getSession();
if (session.get("userInfo") != null) { // 登录成功
String result = invocation.invoke();
return result;
} else {// 未登录
session.put("loginError", "请先登录");
return "login";
} } }
8.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <package name="default" extends="struts-default" namespace="/"> <!-- 注册拦截器 -->
<interceptors>
<interceptor name="auth" class="org.struts.interceptor.AuthInterceptor"/>
<interceptor-stack name="authStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="auth"/>
</interceptor-stack>
</interceptors> <!-- 通过此Action访问后台管理页面 -->
<action name="auth">
<result>/WEB-INF/view/manager.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="authStack"/>
</action> <action name="login" class="org.struts.action.LoginAction">
<result>/WEB-INF/view/manager.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package> </struts>
9.结果预览
9.1 未登录直接访问
9.2 登录成功
9.3 登录成功后访问
参考:http://www.imooc.com/video/9102
最新文章
- html中如何添加提示信息
- ASP模拟POST请求异步提交数据的方法
- ecshop 不同页面调用不同分类文章的解决办法
- 使用Let`s encrypt 免费的https 证书
- Ganglia安装扩容
- php之thinkphp部署Linux
- JRE、JDK和JVM之间的关系
- 传输层之TCP
- mysql 优化点小结
- C++ 线程的创建,挂起,唤醒,终止
- 新手可以学习cocos2dx 3.0 组态(两)
- visual studio 中将选中代码相同的代码的颜色设置,修改高亮颜色
- VMware中Linux系统时间与主机同步以及时区设置
- RMAN还原时注意set newname时文件名不要有空格
- python三种回收机制
- Filezilla server配置FTP服务器中的各种问题与解决方法
- vue——vue-resource
- Asp.net与office web apps的整合
- java 完全解耦
- 用bundler安装jeklly
热门文章
- linux安装mongodb(设置非root用户和开机启动)
- Web Api 返回图片流给前端
- AtCoder - 3962 Sequence Growing Hard
- HashMap源码-描述部分
- Python数据结构:序列(列表[]、元组())与映射(字典{})语法总结
- Android简单的利用MediaRecorder进行录音的实例代码
- UBI介绍
- FIREDAC驱动MYSQL数据库
- 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type &#39;java.lang.String&#39; to required type &#39;java.util.Date&#39; for property &#39;createDate&#39;;
- oracle 被另一个用户锁定