简单的Interceptor例子
给出一个简单的Interceptor的例子。
例子内容描述:在login页面中输入评论标题和评论内容,通过定义的Interceptor,将评论内容中的“讨厌”替换为“喜欢”,并将结果输出的success页面中。
login.jsp的源代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="36"/>
<s:textarea name="content" cols="36" rows="6" label="评论内容"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
在struts.xml文件中声明要使用的Interceptor。代码为:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="replace" class="interceptor.MyInterceptor"/>
</interceptors>
<action name="public" class="interceptor.PublicAction">
<result name="success">success.jsp</result>
<result name="login">login.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="replace"/>
</action>
</package>
</struts>
在这个xml中,在package的标签内,如果有Interceptor,则要先定义Interceptors标签,并在该标签内,定义具体的Interceptor的名字和实现的类。在results后面,放入刚刚定义的Interceptor的饮用,使用interceptor—ref 的name对应之前在Interceptor标签的name,表明调用该Action需要执行触发器。一般将默认触发器放在一个interceptor—ref中,然后依次放入其他的interceptor。
被调用的Action代码
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
/**
*
* @author cuixiaohui
*/
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 static Logger getLOG() {
return LOG;
}
public static void setLOG(Logger LOG) {
ActionSupport.LOG = LOG;
}
public PublicAction() {
}
public String execute() throws Exception {
return SUCCESS;
}
}
这个代码没有太多解释的内容,只是为了测试而建立的一个无条件跳转Action
重点看一个Interceptor的写法。
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
*
* @author cuixiaohui
*/
public class MyInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation ai) throws Exception {
Object object = ai.getAction();
if(object != null){
if(object instanceof PublicAction){
PublicAction ac = (PublicAction)object;
String content = ac.getContent();
if(content.contains("讨厌")){
content = content.replaceAll("讨厌", "喜欢");
ac.setContent(content);
}
return ai.invoke();
}else{
return Action.LOGIN;
}
}
return Action.LOGIN;
}
}
首先,自己写的Interceptor实现了AbstractInterceptor,这个类实现了Interceptor接口的大部分方法,继承该方法只需要实现interceptor方法即可。
此外,通过调用ActionInvocation的实例,可以获得触发的action。
在Interceptor中,返回的过程与定义Struts2中Action相同。
最后,是输出页面success.jsp。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
评论标题:<s:property value="title"/>
评论内容:<s:property value="content"/>
</body>
</html>