Struts2 拦截器(二十三)

1.什么是拦截器?
拦截器(interceptor) 是动态拦截Action调用的对象,在执行Action的业务逻辑处理方法execute 之前 ,Struts2中会首先执行struts.xml中引用的拦截器。

[b][color=red]Action拦截器[/color][/b]
2.做登录拦截用户名是否输入 两种不通的拦截器
LoginInterceptor.java

package com.sh.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation ai) throws Exception {
Map session=ai.getInvocationContext().getSession();
String username=(String) session.get("loginName");
if(username!=null && username.length()>0){
return ai.invoke();
}
else {
ActionContext ac=ai.getInvocationContext();
ac.put("popedom", "您还没有登录,请登录!");
return Action.LOGIN;
}

}

}

LoginInterceptor2.java

package com.sh.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.sun.xml.internal.fastinfoset.stax.events.Util;

public class LoginInterceptor2 implements Interceptor {

public void destroy() {
// TODO Auto-generated method stub

}

public void init() {
// TODO Auto-generated method stub

}

public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac=ActionContext.getContext();
String userName=(String)ac.getSession().get("loginName");
if(!Util.isEmptyString(userName)){
return invocation.invoke();
}else{
ac.put("error", "请输入用户名!");
return Action.LOGIN;
}
}

}




4.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="default" extends="struts-default">
<interceptors>
<interceptor name="checkLogin" class="com.sh.interceptor.LoginInterceptor"/>
<interceptor name="checkLogin2" class="com.sh.interceptor.LoginInterceptor2"/>
</interceptors>
<action name="checkLogin" class="com.sh.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="checkLogin2"/>
</action>
</package>

</struts>


5.login.jsp


<body>
<center>
<h3>
${popedom}
${error}
</h3>
<s:form action="checkLogin" method="post">
<s:textfield name="loginName" label"loginName" />
<s:textfield name="loginPassword" label="loginPassword"/>
<s:submit/>
<s:token/>
</s:form>
</center>
</body>


[b][color=red]文字拦截器[/color][/b]
实例:网站论坛要求会员发帖的内容不能脏字,如果会员使用不文明语言,通常情况下,系统会自动使用“***”来替换。在Struts2Hogn可以使用拦截器来实现这个功能。 案例拦截 "不"

ContentInterceptor.java

package com.sh.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.sh.action.publicAction;

public class ContentInterceptor extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object obj=invocation.getAction();
if(obj!=null){
if(obj instanceof publicAction){
publicAction pa=(publicAction)obj;
String content=pa.getContent();
if(content.contains("不")){
content=content.replaceAll("不", "**");
pa.setContent(content);
}
return invocation.invoke();
}else {
return Action.LOGIN;
}
}else{
return Action.LOGIN;
}
}

}



struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="default" extends="struts-default">
<interceptors>
<interceptor name="checkLogin" class="com.sh.interceptor.LoginInterceptor"/>
<interceptor name="checkLogin2" class="com.sh.interceptor.LoginInterceptor2"/>
<interceptor name="replace" class="com.sh.interceptor.ContentInterceptor"/>
</interceptors>
<action name="checkLogin" class="com.sh.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="checkLogin2"/>
</action>
<action name="public" class="com.sh.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>

news.jsp

<body>
<s:form action="public" method="post">
<s:textfield name="title" label="标题" />
<s:textarea name="content" cols="30" rows="5" label="内容"/>
<s:submit/>
<s:token/>
</s:form>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值