自定义拦截器:一些与系统逻辑相关的通用功能,需要通过自定义拦截器来实现(权限的限制,用户输入内容的限制)
1.新建项目,在项目中导入如下的jar包
2. 在web.xml文件中配置struts2的核心控制器,用来拦截客户请求,把请求转发给相应的Action类来处理
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3在webroot文件夹下创建视图页面
(1) news.jsp success.jsp
<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'news.jsp' starting page</title>
</head>
<body>
<center>
网友评论<br/>
<s:form action="public" method="post">
<s:textfield name="title" label="标题" maxLength="20"></s:textfield>
<s:textfield name="content" cols="40" rows="5" label="内容"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</center>
</body>
</html>
<%@taglib prefix="s" uri="/struts-tags" %>上面代码中,首先用taglib指令定义uri属性,该属性值引入struts2框架
(2)success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
<s:property value="title"/>
<s:property value="content"/>
</body>
</html>
4. 定义处理请求的Action
package action;
import com.opensymphony.xwork2.ActionSupport;
public class publicAction extends ActionSupport{
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String execute(){
return SUCCESS;
}
}
title和content两个属性分别代表评论标题和评论内容,然后返回一个SUCCESS逻辑视图
5. 实现自定义拦截器
struts2 提供interceptor的接口,通过接口实现一个拦截器类,此接口包含三个方法:
(1) init() 拦截器执行之前,用于初始化系统资源
(2)destroy() 拦截器执行之后销毁资源
(2)interceptor() 拦截器的核心方法,实现具体的拦截操作,返回一个字符串作为逻辑视图
public String intercept(ActionInvocation ai) throws Exception
ActionInvocation包含被拦截的Action应用,调用action的invoke()方法,将控制权交给下一个拦截器或者action的excute()方法。
struts2 提供了一个抽象拦截器类(AbstractIntercptor) 这个类对init()和destroy()进行空实现,只实现interceptor()方法就行
package interceptor;
import action.publicAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class Myinterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
//获取action实例
Object obj = ai.getAction();
if(obj!=null){
if(obj instanceof publicAction){
publicAction action = (publicAction)obj;
String content = action.getContent();
if(content.contains("草")){
content = content.replaceAll("草", "*");
action.setContent(content);
}
return ai.invoke();
}else{
return Action.LOGIN;
}
}else{
return Action.LOGIN;
}
}
}
通过框架传递过来的ActionInvocation 对象,通过这个对象可以获得action对象,用户提交的数据都保存在action对象,在action对象里面的content属性值来检查1是否包含需要过滤的文字
6. 接下来在struts.xml文件里配置Action
<?xml version="1.0" encoding="UTF-8"?>
<!--下面是Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>
<package name="testIntercptor" namespace="/" extends="struts-default">
<!-- 定义拦截器 -->
<interceptors>
<interceptor name="replace" class="interceptor.Myinterceptor"/>
</interceptors>
<action name="public" class="action.publicAction">
<result name="success">/success.jsp</result>
<result name="login">/news.jsp</result>
<!-- 使用拦截器 -->
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="replace"/>
</action>
</package>
</struts>
(注意:struts.xml文件放在src目录下)