Struts2 custom interceptor and struts2 custom Interception
The first is the two main files in the struts2 framework: one is the interceptor class and the other is the struts. xml configuration file.
To put it simply, one is to explain what to do, and the other is to explain where to do it.
Interceptor class
Package com. test. interceptor; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor; public class TestInterceptor extends actinterceptor {/*****/private static final long serialVersionUID = 1L; @ Overridepublic String intercept (ActionInvocation invocation) throws Exception {System. out. println ("before Interceptor ........... "); String result = invocation. invoke (); System. out. println (".......... After the interceptor. "); Return result ;}}
This interceptor is very simple, that is, two paragraphs are output before and after all actions.
Struts. xml 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. enable. dynamicMethodInvocation "value =" true "/> <constant name =" struts. devMode "value =" true "/> <package name =" default "namespace =" "extends =" struts-default "> <! -- Here is the interceptor configuration --> <interceptors> <interceptor name = "test" class = "com. test. interceptor. testInterceptor "> </interceptor> <interceptor-stack name =" myStack "> <interceptor-ref name =" test "/> <interceptor-ref name =" defaultStack "/> </interceptor-stack> </interceptors> <! -- Correct and wrong jump, respectively. input is the page that will jump after verification fails --> <action name = "data" class = "com. test. action. valideAction "> <interceptor-ref name =" myStack "> </interceptor-ref> <! -- Use the interceptor action --> <result name = "success">/result. jsp </result> <result name = "input">/login. jsp </result> </action> </package> </struts>
Output result
Question about struts2 custom interceptor
The interceptor has nothing to do with struts2. This is configured in web. xml. The following is the so-called Interceptor:
<Filter>
<Filter-name> encodingFilter </filter-name>
<Filter-class> org. springframework. web. filter. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> encodingFilter </filter-name>
<Url-pattern> *. action </url-pattern>
</Filter-mapping>
The following section describes the implementation of the logon Interceptor:
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Request. setCharacterEncoding ("UTF-8 ");
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String currentURL = httpRequest. getRequestURI ();
// Obtain the absolute path corresponding to the root directory:
String targetURL = currentURL. substring (currentURL. lastIndexOf ("/"),
CurrentURL. length ());
// Truncate the current file name for comparison
HttpSession session = httpRequest. getSession (false );
If (! ("/UserLogin! LogIn. action ". equals (targetURL)
| "/Login. jsp". equals (targetURL) | "/login_admin.jsp"
. Equals (targetURL ))){
If (session = null | session. getAttribute ("userInfo") = null ){
// * You must manually add a session after logging on.
HttpResponse. sendRedirect (h ...... remaining full text>
How to execute Struts2 custom interceptor before action
Before an Action, use the Interceptor to directly return act. invoke (). You just need to put the final operation between act. invoke () and return result.
Public class OneInterceptor implements Interceptor {
...
Public String intercept (ActionInvocation act) throws Exception {
// Code executed before the Action
......
// Start to execute the Action
String result = act. invoke ();//
// Code after Action
......
// End the interceptor
Return result;
}
}
It is actually a simple principle, as long as you understand the principle of the interceptor. I did not think of this method. I used google to search for a foreign website ......
In addition, the webmaster group has products to buy, Which is cheap and guaranteed.