解决系统资源不足问题
2006-11-20 19:35
267 查看
一.Struts2使用拦截器步骤:
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java
2. 声明拦截器 struts.xml
3. 使用拦截器 struts.xml
注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器
三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. struts.xml
2. MyInterceptor.java
四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java
2. struts.xml
五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1. MethodFilterInterceptor.java
2. struts.xml
六.拦截器栈(拦截器的集合)
1. struts.xml
七.自定义默认的拦截器
1. struts.xml
注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1. 定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口
2. 在拦截器MyInterceptor中增加监听器
对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:
九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java
2. struts.xml增加拦截器
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
2. 声明拦截器 struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action> </package> </struts>
3. 使用拦截器 struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> </package> </struts>
注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器
<action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action>
三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
</interceptors>
<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>
<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>
<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>
<action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action>
</package>
</struts>
2. MyInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); System.out.println("hello:"+hello); } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.err.println("MyInterceptor2 invoked"); String result=invocation.invoke(); System.out.println("MyInterceptor2 invoke result:"+result); return result; } }
2. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1. MethodFilterInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class MyInterceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("MyInterceptor3 invoked"); String result=invocation.invoke(); System.out.println("MyInterceptor3 invoke result:"+result); return result; } }
2. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="myInterceptor3"> <!-- <param name="excludeMethods">execute</param> --> 没有param就拦截所有方法,includeMethods拦截指定方法,excludeMethods不拦截指定方法 </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
六.拦截器栈(拦截器的集合)
1. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> </interceptor-stack> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> --> <interceptor-ref name="myStack"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
七.自定义默认的拦截器
1. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="myStack"></interceptor-ref> --> </action> </package> </struts>
注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1. 定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口
package com.test.listener; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyListener implements PreResultListener { @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("MyListener invoked"); } }
2. 在拦截器MyInterceptor中增加监听器
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.test.listener.MyListener; public class MyInterceptor implements Interceptor { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); System.out.println("hello:"+hello); } @Override public String intercept(ActionInvocation invocation) throws Exception { invocation.addPreResultListener(new MyListener()); System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:
@Override public String intercept(ActionInvocation invocation) throws Exception { invocation.addPreResultListener(new PreResultListener(){ @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("hello world"); } }); System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; }
九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java
package com.test.interceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthInterceptor extends AbstractInterceptor { @SuppressWarnings("unchecked") @Override public String intercept(ActionInvocation invocation) throws Exception { Map map=invocation.getInvocationContext().getSession(); if(null==map.get("user")){ //用户没有登录 return Action.LOGIN; }else{ return invocation.invoke(); //转到下一个拦截器 } } }
2. struts.xml增加拦截器
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> //声明权限拦截器 <interceptor name="authInterceptor" class="com.test.interceptor.AuthInterceptor"> </interceptor> //使用权限拦截器 <interceptor-stack name="myStack"> <interceptor-ref name="authInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> //使用全局结果集,对所有Action生效 <global-results> //type的值,可以在struts-default.xml中查找,redirect是重定向,默认是dispatcher转发 <result name="login" type="redirect">/login.jsp</result> </global-results> <!-- <default-interceptor-ref name="myStack"></default-interceptor-ref> --> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> --> <interceptor-ref name="myStack"></interceptor-ref> </action> </package> </struts>
相关文章推荐
- Windows 系统资源不足问题的解决
- 解决IDEA中进行maven install报:系统资源不足的问题
- 解决系统资源不足问题
- 解决 Mysql 占用系统资源过大的问题
- 通过删除旧内核来解决系统升级/boot空间不足的问题
- 解决Linux系统下以PL/SQL工具以sysdba登录报ora-01031权限不足问题
- 解决win10系统提示“计算机内存不足”的问题?
- 解决系统资源严重不足
- 解决windows系统因TCP端口不足导致mysql数据库无法访问的问题
- Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决
- win10开机提示“系统资源不足无法完成API”的解决教程
- VMWare11虚拟机安装OSX10.9系统资源下载及问题解决
- 解决Linux下pcieport 0000:00:1c.5问题导致的系统根目录/磁盘空间不足
- 电脑公司Win7系统提示系统资源不足警告的两个解决方法
- 如何优化代码节约系统资源解决重复实例化对象的问题——神奇的单例模式(C#设计模式)
- Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决
- “系统资源不足,无法满足请求服务“ ,而内存够大,解决办法
- 巧用双系统并存解决硬件不足问题 推荐
- ubuntu文件系统分区调整(解决目录空间不足、分配不均问题)
- 解决Linux系统因TCP端口不足导致mysql数据库无法访问的问题