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><!--override Struts2 Some constants default value--<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><!-- Just define the Interceptor--><interceptor name= "Timecost" class= "Com.itheima.interceptor.TimeCostInterceptor" ></ Interceptor><interceptor name= "Logincheck" class= "Com.itheima.interceptor.LoginCheckInterceptor" >< param name= "excludemethods" >sayHello1</param><!--excluded from blocking--></interceptor><!--Definition group--><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;//statistics action Method Execution Efficiency Interceptor public class Timecostinterceptor extends Abstractinterceptor {//Core intercept method public String intercept (actioninvocation invocation) throws Exception {Long startTime = System.nanotime ();//nanosecond: 1s=1000000000 nanosecond string result = Invocation.invoke ();//Let the next execution 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" >
login.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Struts2 Landing blocker (FIX)