struts2 的拦截器的应用
2007-04-23 11:43
387 查看
和webwork得差不多,首先写拦截器配置文件:xwork-losbgm-default.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<xwork>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="struts-default.xml" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<package name="xwork-losbgm-default" extends="struts-default">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptors>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor name="employeeAccessInterceptor"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="tool.UserAccessInterceptor">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptor>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-stack name="losbgmStack">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-ref name="employeeAccessInterceptor"></interceptor-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-ref name="defaultStack" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptor-stack>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptors>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<default-interceptor-ref name="losbgmStack"></default-interceptor-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-results>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="error">/error.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="index">/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</global-results>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</package>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</xwork>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在写一个拦截器类:UserAccessInterceptor.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Source file: F:/java/Oil/src/com/xj/tools/UserAccessInterceptor.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package tool;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.opensymphony.xwork2.ActionInvocation;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UserAccessInterceptor extends AbstractInterceptor ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroy() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.destroy();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void init() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String intercept(ActionInvocation arg0) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "error";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
再要实现拦截器的配置文件中先倒入文件再继承包就可以了:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8" ?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE struts PUBLIC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://struts.apache.org/dtds/struts-2.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<struts>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="struts-default.xml"></include>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="xwork-losbgm-default.xml"></include>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<package name="login" namespace="/" extends="xwork-losbgm-default">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="login" class="action.LoginAction">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="success">/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="main">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/main.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="Register">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="*">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/{1}.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</package>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</struts>
===================================================================================
struts2中的拦截器如何配置在action前后都执行呢?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public String intercept(ActionInvocation invocation) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//do some job before invocation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String result = invocation.invoke();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//do some job after invocation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
==============================================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<xwork>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="struts-default.xml" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<package name="xwork-losbgm-default" extends="struts-default">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptors>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor name="employeeAccessInterceptor"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="tool.UserAccessInterceptor">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptor>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-stack name="losbgmStack">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-ref name="employeeAccessInterceptor"></interceptor-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<interceptor-ref name="defaultStack" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptor-stack>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</interceptors>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<default-interceptor-ref name="losbgmStack"></default-interceptor-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-results>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="error">/error.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="index">/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</global-results>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</package>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</xwork>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在写一个拦截器类:UserAccessInterceptor.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Source file: F:/java/Oil/src/com/xj/tools/UserAccessInterceptor.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package tool;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.opensymphony.xwork2.ActionInvocation;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UserAccessInterceptor extends AbstractInterceptor ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroy() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.destroy();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void init() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String intercept(ActionInvocation arg0) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "error";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
再要实现拦截器的配置文件中先倒入文件再继承包就可以了:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8" ?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE struts PUBLIC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://struts.apache.org/dtds/struts-2.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<struts>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="struts-default.xml"></include>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<include file="xwork-losbgm-default.xml"></include>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<package name="login" namespace="/" extends="xwork-losbgm-default">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="login" class="action.LoginAction">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result name="success">/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="main">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/main.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="Register">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/index.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action name="*">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<result>/{1}.jsp</result>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</package>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</struts>
===================================================================================
struts2中的拦截器如何配置在action前后都执行呢?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public String intercept(ActionInvocation invocation) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//do some job before invocation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String result = invocation.invoke();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//do some job after invocation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
==============================================================================
相关文章推荐
- Struts2拦截器的应用
- 拦截器在Struts2中的缺省应用
- Struts2拦截器的应用
- 拦截器在struts2中的应用
- Struts2拦截器的应用:未登录用户拦截(同时拦截ajax请求)
- Struts2拦截器的一个简单应用
- SSH—Struts2拦截器的应用(防止未登录用户进行操作)
- struts2应用技术层,拦截器,struts2标签
- struts2中过滤器和拦截器的区别,以及他们所应用的事例
- struts2中自定义拦截器的简单应用
- Struts2之拦截器的简单应用
- Struts2拦截器的应用:文件上传
- Struts2基础应用4——自定义拦截器
- Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】
- struts2拦截器应用
- struts2拦截器应用wait
- struts2拦截器的方法过滤功能在Action动态方法调用中的应用 .
- Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】
- SSH网上商城--Struts2拦截器的应用
- 第四课 拦截器在Struts2中的缺省应用