Struts2 interceptor configuration

<?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>
	<constant name="struts.devMode" value="true" />
	<package name="p1" extends="struts-default">
		<!-- declare custom interceptor -->
		<interceptors>
			<interceptor name="demo1Interceptor" class="com.itheima.web.interceptor.Demo1Interceptor"></interceptor>
			<interceptor name="demo2Interceptor" class="com.itheima.web.interceptor.Demo2Interceptor"></interceptor>
		</interceptors>
		<action name="action1" class="com.itheima.web.action.Demo1Action" method="save">
			<!-- Use custom interceptor: When any interceptor is configured, the default interceptor stack will not work.
				When there are multiple interceptors, the order of execution is determined by the reference configuration. Note: Execution order has nothing to do with declarations -->
			<interceptor-ref name="demo2Interceptor"></interceptor-ref>
			<interceptor-ref name="demo1Interceptor"></interceptor-ref>
			<result name="success">/demo1.jsp</result>
		</action>
	</package>
	
	<!-- Use a custom interceptor to check for logins. The most basic configuration.
		 Among the problems:
		 	When we use a custom interceptor, the default interceptor stack doesn't work anymore
	<package name="p2" extends="struts-default">
		<interceptors> declares interceptors
			<interceptor name="checkLoginInterceptor" class="com.itheima.web.interceptor.CheckLoginInterceptor" />
		</interceptors>
		<global-results> global results view
			<result name="input">/login.jsp</result> Result view of data echo
		</global-results>
		When the user is logged in, the interceptor that does not need to check the login works
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<result type="redirectAction">showMain</result>
		</action>
		The name of the action to go to the home page, the interceptor that needs to check the login works
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			<result>/main.jsp</result>
		</action>
		The action name of going to another page, the interceptor that needs to check the login works
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			<result>/otherpage.jsp</result>
		</action>
	</package>-->
	
	<!-- a. For the above problem, our solution is to configure the default interceptor stack as well.
	<package name="p2" extends="struts-default">
		<interceptors>
			<interceptor name="checkLoginInterceptor" class="com.itheima.web.interceptor.CheckLoginInterceptor" />
		</interceptors>
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<result type="redirectAction">showMain</result>
		</action>
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			Add custom interceptor, and default interceptor stack
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			<result>/main.jsp</result>
		</action>
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			Add custom interceptor, and default interceptor stack
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			<result>/otherpage.jsp</result>
		</action>
	</package>-->
	
	<!-- b. The drawbacks of a. When there are many actions to be intercepted, it is very cumbersome to write
	<package name="p2" extends="struts-default">
		<interceptors>
			<interceptor name="checkLoginInterceptor" class="com.itheima.web.interceptor.CheckLoginInterceptor" />
			Define an interceptor stack, put our custom interceptor and the default interceptor stack together.
			<interceptor-stack name="myDefaultStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<result type="redirectAction">showMain</result>
		</action>
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			Directly introduce our own defined interceptor stack, which already contains the default interceptor stack
			<interceptor-ref name="myDefaultStack"></interceptor-ref>
			<result>/main.jsp</result>
		</action>
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			Directly introduce our own defined interceptor stack, which already contains the default interceptor stack
			<interceptor-ref name="myDefaultStack"></interceptor-ref>
			<result>/otherpage.jsp</result>
		</action>
	</package> -->
	
	<!-- The problem in c.b also requires the use of interceptor references in every place it is used.
		 Solution: Use overriding the default interceptor stack in the struts-default.xml configuration file, let's call this the default one
	
	<package name="p2" extends="struts-default">
		<interceptors>
			<interceptor name="checkLoginInterceptor" class="com.itheima.web.interceptor.CheckLoginInterceptor" />
			<interceptor-stack name="myDefaultStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		Overrides the default interceptor stack defined in struts-default.xml. Replaced defaultStack by myDefaultStack
		<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<result type="redirectAction">showMain</result>
		</action>
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			<result>/main.jsp</result>
		</action>
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			<result>/otherpage.jsp</result>
		</action>
	</package> -->
	
	<!-- The problem in d.c, when we configured the default interceptor stack, even the login was intercepted.
		Solution: In the subclass of AbstractInterceptor, there is also an abstract class MethodFilterInterceptor, which provides two properties.
				excludeMethods: which methods do not need to be intercepted
				includeMethods: which methods need to be intercepted
	 
	<package name="p2" extends="struts-default">
		<interceptors>
			<interceptor name="checkLoginInterceptor1" class="com.itheima.web.interceptor.CheckLoginInterceptor1" />
			<interceptor-stack name="myDefaultStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="checkLoginInterceptor1">
					Inject parameters into the custom interceptor to tell him which methods do not need to be intercepted
					<param name="excludeMethods">login</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<result type="redirectAction">showMain</result>
		</action>
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			<result>/main.jsp</result>
		</action>
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			<result>/otherpage.jsp</result>
		</action>
	</package> -->
	
	<!-- The problem in e.d, when we declare the interceptor and define the interceptor stack, we may not know which methods need to be intercepted and which do not need to be intercepted.
		Solution: When using an interceptor, inject parameters to tell the interceptor which ones need to be intercepted and which ones don't.
	 -->
	<package name="p2" extends="struts-default">
		<interceptors>
			<interceptor name="checkLoginInterceptor1" class="com.itheima.web.interceptor.CheckLoginInterceptor1" />
			<interceptor-stack name="myDefaultStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="checkLoginInterceptor1"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		
		<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
			<interceptor-ref name="myDefaultStack">
				<!-- When referring to the custom interceptor stack, inject parameters into the specified interceptor. The way is: interceptor name. attribute name -->
				<param name="checkLoginInterceptor1.excludeMethods">login</param>
			</interceptor-ref>
			<result type="redirectAction">showMain</result>
		</action>
		<action name="showMain" class="com.itheima.web.action.Demo2Action" >
			<result>/main.jsp</result>
		</action>		
		<action name="showOther" class="com.itheima.web.action.Demo2Action" >
			<result>/otherpage.jsp</result>
		</action>
	</package>
</struts>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326641440&siteId=291194637