标签:配置 main 返回 pac oid 其他 应用 open 开发
<body> <a href="<%=path%>/Interceptor/InterceptorAction">此页面跳转的action会被自定义拦截器拦截</a> </body>
<package name="default" namespace="/Interceptor" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="interceptorTest" class="interceptor.InterceptorTest"></interceptor> </interceptors> <action name="InterceptorAction" class="action.InterceptorAction"> <result name="re">/index_2.jsp</result> <!-- 引用拦截器 --> <interceptor-ref name="interceptorTest"></interceptor-ref> </action> </package>
package interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; /** * 自定义的拦截器 * @author Administrator * */ public class InterceptorTest implements Interceptor { public void destroy() { } public void init() { } public String intercept(ActionInvocation actionInterceptor) throws Exception { System.out.println("我是第一次自定义拦截器"); /* * 让Action的方法执行,并返回action的结果。 * ActionInvocation。invoke()方法会调用下一个拦截器 或 执行下一个Action,如果是执行actino就返回action的result值。 * 而ActionInvocation。getResultCode()只获得action的result返回值并不执行action。 */ String next=actionInterceptor.invoke(); return null; } }
package action; public class InterceptorAction { public String execute(){ System.out.println("我是action"); return "re"; } }
<body>
我是跳转后的页面
</body>
结果:
查看后台可知,拦截器中的方法比action的方法先执行,说明拦截成功。
<package name="default" namespace="/Interceptor" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="interceptorTest" class="interceptor.InterceptorTest"></interceptor> <interceptor name="interceptor_two" class="interceptor.Interceptor_two"></interceptor> <!-- 定义拦截器栈,引入拦截器 --> <interceptor-stack name="mystack"> <interceptor-ref name="interceptor_two"></interceptor-ref> <interceptor-ref name="interceptorTest"></interceptor-ref> </interceptor-stack> </interceptors> <action name="InterceptorAction" class="action.InterceptorAction"> <result name="re">/index_2.jsp</result> <!-- 引用拦截器栈--> <interceptor-ref name="mystack"></interceptor-ref> </action> </package>
<package name="default" namespace="/Interceptor" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="interceptorTest" class="interceptor.InterceptorTest"></interceptor> <interceptor name="interceptor_two" class="interceptor.Interceptor_two"></interceptor> <!-- 设定哪一个拦截器或者拦截器栈为默认的 1:当Action中没有使用interceptor-ref引用时,默认拦截器将会起作用。如果Action中配置interceptor-ref,那么 默认的拦截器将不起作用 2:将设定default-interceptor-ref为自定义的拦截器后,struts-default.xml中定义的默认拦截器将不起作用。 所有Struts的功能将不能使用。要将defaultStack拦截器栈引用到自定义到拦截器栈中 3:其他的package要使用自定义的拦截器,将package做一个继承即可。 --> <interceptor-stack name="mystack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="interceptor_two"></interceptor-ref> <interceptor-ref name="interceptorTest"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="mystack"></default-interceptor-ref> <action name="InterceptorAction" class="action.InterceptorAction"> <result name="re">/index_2.jsp</result> </action> </package>
1:当Action中没有使用interceptor-ref引用时,默认拦截器将会起作用。如果Action中配置interceptor-ref,那么
默认的拦截器将不起作用
2:将设定default-interceptor-ref为自定义的拦截器后,struts-default.xml中定义的默认拦截器将不起作用。
所有Struts的功能将不能使用。要将defaultStack拦截器栈引用到自定义到拦截器栈中。
3:其他的package要使用自定义的拦截器,将package做一个继承即可。
4. Struts2在struts-default包中定义了一个默认的拦截器栈引用:defaultStack。因此当我们定义的包继承自struts-default包时也继承了它的默认拦截器引用:defaultStack,当我们不为Action显式的应用拦截器,则defaultStack栈会起作用。
<body> <h2>登录系统</h2> <form action="interceptor/main" method="post"> 用户名:<s:textfield name="username"></s:textfield><br/> 密 码:<s:password name="password"></s:password> <s:submit value="提交"></s:submit> </form> </body>
<package name="default" namespace="/interceptor" extends="struts-default"> <interceptors > <interceptor name="login" class="interceptor.LoginInterceptor"></interceptor> </interceptors> <action name="main" class="action.GoMainAction"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="login"></interceptor-ref> <result name="main">/index_2.jsp</result> </action> </package>
package action; import javax.servlet.http.HttpSession; import org.omg.CORBA.Request; import com.opensymphony.xwork2.ActionSupport; import actionUtil.BaseAction; public class GoMainAction extends BaseAction{ /* * 在action中获取表单值的前提是必须在struts.xml中引用默认拦截器引用:defaultStack */ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute(){ if((this.username).equals("admin")){ if((this.password).equals("123")){ HttpSession session=request.getSession(); session.setAttribute("login", "ok"); return "main"; }else{ System.out.println("密码错误"); return null; } }else{ System.out.println("用户名错误"); return null; } } }
LoginInterceptor.java
package interceptor; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.util.ValueStack; import action.GoMainAction; public class LoginInterceptor implements Interceptor{ public void destroy() { } public void init() { } public String intercept(ActionInvocation actionInvocation) throws Exception { Object action=actionInvocation.getAction(); //拿到被拦截的action对象 ActionContext context=actionInvocation.getInvocationContext(); //拿到action的广义值栈。 HttpServletRequest request=ServletActionContext.getRequest(); //拿到request对象 HttpServletResponse response = ServletActionContext.getResponse(); //拿到response对象 Map<String,Object> sessionMap=context.getSession(); // ValueStack valuestack=context.getValueStack(); //拿到action的狭义值栈。 if(action.getClass().equals(GoMainAction.class)){ //如果是验证登录的action,。则不进行拦截 actionInvocation.invoke(); return null; }else{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String loginURL = request.getContextPath() + "/index.jsp"; out.println("<script language=‘javascript‘>"); out.println("window.alert(‘您还未登录,或者登录已超时,请重新登录‘);"); out.println("window.location.href = ‘" + loginURL + "‘;"); out.println("</script>"); return null; } } }
<body>
登录成功
</body>
2.5 利用拦截器实现监听程序运行效率(监听每个方法的运行时间,时间越长代表效率越低)
<body> <a href="interceptor/TestAction!one">方法1的运行效率</a> <a href="interceptor/TestAction!two">方法2的运行效率</a> </body>
<package name="default" namespace="/interceptor" extends="struts-default"> <interceptors> <interceptor name="test" class="interceptor.TestInterceptor"></interceptor> </interceptors> <action name="TestAction" class="action.TestAction"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="test"></interceptor-ref> </action> </package>
TestAction.java
package action; public class TestAction { public String execute(){ return null; } /** * 要测试的方法1 * @return */ public String one(){ System.out.println("运行one方法"); return null; } /** * 要测试的方法2 * @return */ public String two(){ System.out.println("运行two方法"); try { Thread.sleep(6000); //停留6秒 } catch (InterruptedException e) { e.printStackTrace(); } return null; } }
package interceptor; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.util.ValueStack; public class TestInterceptor implements Interceptor{ public void destroy() { } public void init() { } public String intercept(ActionInvocation actionInvocation) throws Exception { Object obj=actionInvocation.getAction(); //获取正在拦截的对象 ActionContext actionContext=actionInvocation.getInvocationContext(); //获取被拦截action的上下文对象 ValueStack valuestack=actionContext.getValueStack(); //获取action的狭义值栈 HttpServletRequest request = ServletActionContext.getRequest(); //获取erquest对象 StringBuffer requestURL=request.getRequestURL(); //获取超时方法的URL //让action方法执行,并测试 long start=System.currentTimeMillis(); //获取方法执行前的时间 actionInvocation.invoke(); //执行方法 long end=System.currentTimeMillis(); //获取方法执行后的时间 int diffSecond = (int) ((end - start) / 1000); //获取方法总共执行时间差 if(diffSecond >5){ System.out.println("requestURI= " + requestURL +",间隔时间= "+diffSecond); //如果方法执行时间超过5秒就把这个URL打印出来 } return null; } }
结果:
标签:配置 main 返回 pac oid 其他 应用 open 开发
原文地址:http://www.cnblogs.com/shyroke/p/6606624.html