struts2.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> <!-- 覆盖掉Struts2一些常量的默认值 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action,,do"></constant> <constant name="struts.configuration.xml.reload" value="true"></constant> <constant name="struts.locale" value="zh_CN"></constant> <package name="mydefaultpackage" extends="struts-default"> <interceptors><!-- 只是定义拦截器 --> <interceptor name="timeCost" class="com.itheima.interceptor.TimeCostInterceptor"></interceptor> <interceptor name="loginCheck" class="com.itheima.interceptor.LoginCheckInterceptor"> <param name="excludeMethods">sayHello1</param><!-- 排除在拦截之外 --> </interceptor> <!-- 定义小组 --> <interceptor-stack name="mydefaultStack"> <interceptor-ref name="annotationWorkflow"></interceptor-ref> <!-- <interceptor-ref name="loginCheck"></interceptor-ref> --> <interceptor-ref name="timeCost"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="mydefaultStack"></default-interceptor-ref> </package> <package name="p1" extends="mydefaultpackage"> <action name="test1" class="com.itheima.actions.ActionDemo1"> <result>/index.jsp</result> <result name="loginCheckFailure">/failure.jsp</result> </action> <action name="test2" class="com.itheima.actions.ActionDemo2"> <result>/index.jsp</result> </action> </package> </struts>
TimeCostInterceptor
package com.itheima.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; //统计动作方法执行效率的拦截器 public class TimeCostInterceptor extends AbstractInterceptor { //核心拦截方法 public String intercept(ActionInvocation invocation) throws Exception { long startTime = System.nanoTime();//纳秒: 1s=1000000000纳秒 String result = invocation.invoke();//让下一个执行 long endTime = System.nanoTime(); System.out.println(invocation.getInvocationContext().getName()+" cost time :"+(endTime-startTime)+" ns"); return result; } }
LoginCheckInterceptor
package com.itheima.interceptor; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class LoginCheckInterceptor extends MethodFilterInterceptor { protected String doIntercept(ActionInvocation invocation) throws Exception { String user = (String)ServletActionContext.getRequest().getSession().getAttribute("user"); String result = "loginCheckFailure"; if(user!=null){ result = invocation.invoke(); } return result; } }
failure.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> 对不起!您没有登陆<a href="${pageContext.request.contextPath}/login.jsp?username=wzhting">登陆</a> </body> </html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <% String username = request.getParameter("username"); if(username!=null){ session.setAttribute("user", username); }else{ session.invalidate();//相当于注销 } %> </body> </html>