Struts2 Learning Log (1): Interceptor, struts2 log

Source: Internet
Author: User

Struts2 Learning Log (1): Interceptor, struts2 log
Implementation of interceptor principle (Dynamic proxy ):

Package Struts. test. proxy; // function execution class public class ExecuteFunction implements ExecuteFunctionInterface {public void execute () {// execution function class execution method System. out. println ("execute something ...... ");}}
Package Struts. test. proxy; // Interceptor class public class Interceptor {public void beforeDoing () {// method of execution before intercepting the execution function class System. out. println ("before doing Something ...... ");} Public void afterDoing () {// Method for blocking execution of function classes and then executing System. out. println (" after doing Something ...... ");}}

 

Package Struts. test. proxy; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; // Interceptor processing class public class InterceptorHandler implements InvocationHandler {private Object object Object; private Interceptor inter = new Interceptor (); public void setObject (object Object object) {// set the execution function class to be intercepted this. object = object;} // interface invoke method. proxy is the proxy instance, Method is the instance method, and args is the method parameter passed in by the proxy class public Object invoke (Object proxy, Method method, object [] args) throws Throwable {inter. beforeDoing (); // object provides class instances for this method. args is the array Object returnObject = method that calls the parameter values required by the method. invoke (object, args); inter. afterDoing (); return returnObject ;}}

 

Package Struts. test. proxy; import java. lang. reflect. proxy; // Proxy Object class public class ProxyObject {private InterceptorHandler handler = new InterceptorHandler (); public Object getProxy (object Object) {// get the Proxy instance handler of the execution class. setObject (object); // create an object instance return Proxy. newProxyInstance (ExecuteFunction. class. getClassLoader (), object. getClass (). getInterfaces (), handler );}}
 
Package Struts. test. proxy; public class TestInterceptor {public static void main (String [] args) {ExecuteFunctionInterface test = new ExecuteFunction (); // get an execution class proxy instance ExecuteFunctionInterface resultObject = (ExecuteFunctionInterface) new ProxyObject (). getProxy (test); resultObject.exe cute (); // execute the proxy object }}

 

Execution result: Configure the custom Interceptor: 1. Extended interceptor InterfaceImplement the Interceptor Interface 2. inherit the abstract interceptorInherits the AbstractInterceptor abstract class and overwrites the intercept () method.
3. inherit method interceptorInherit the abstract class MethodFilterInterceptor and override the doIntercept () method.
<! ------------------------- File name: struts. xml -------------------------> <struts> <! -- Action package definition --> <package name = "C04.3" extends = "struts-default"> <! -- Interceptor configuration definition --> <interceptors> <interceptor name = "example" class = "com. example. struts. interceptor. ExampleInterceptor"> </interceptor> </interceptors> <! -- Default interceptor stack configuration definition <default-interceptor-ref name = "example"> </default-interceptor-ref> --> <! -- Action name, class, and navigation page definition --> <! -- Define the Action to be navigated through Action class processing --> <action name = "Login" class = "com. example. struts. action. loginAction "method =" method "> <result name =" input ">/jsp/login. jsp </result> <result name = "success">/jsp/success. jsp </result> <! -- Action Method interceptor configuration definition --> <interceptor-ref name = "example"> <! -- Definition of intercepted method Configuration --> <param name = "includeMethods"> method </param> <! -- Definition of non-blocked method Configuration --> <param name = "excludeMethods"> method, execute </param> </interceptor-ref> </action> </package> </struts> 
File Upload: In JSP Form:1. <s: form action = "upload" method = "post" enctype = "multipart/form-data">. The parameters of method and enctype must be shown above. 2. Create a root directory for storing uploaded files in the web directory. 3. Define three parameters in action: file, fileFileName, and fileContentType. The file before each parameter is the value in the name attribute of the file tag uploaded in the jsp form. 4. when configuring the interceptor, you can configure two parameters: allowType: allowExtensions of the file to be uploaded according to the file type constraints: restrict the uploaded file according to the file suffix 5. if it is displayed that the default interceptor configured for Struts 2 must be written before "defaultStack", otherwise the interceptor defined by myself will not be intercepted. File Download: 1. the type attribute is defined in the <result> label and the value is "stream ". 2. contentDisposition: 3. // read the file output stream from the original storage path of the downloaded file
public InputStream getDownloadFile(){returnServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileName);}  

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.