1 Struts2 Working principle
The processing of a request in the STRUTS2 framework is roughly divided into the following steps:
1. The client initializes a request to the servlet container (for example, Tomcat);
2. This request passes through a series of filters (the filter has an optional filter called Actioncontextcleanup, which is useful for integration of Struts2 and other frameworks, e.g. sitemeshplugin);
3, then Filterdispatcher is called, Filterdispatcher asked Actionmapper to decide whether this need to call an action.
4. If Actionmapper decides to call a action,filterdispatcher, give actionproxy the processing of the request.
5, Actionproxy through ConfigurationManager Ask the framework configuration file, find the action class that needs to be called.
6. Actionproxy create an instance of Actioninvocation.
7. The Actioninvocation instance is invoked using a naming pattern, which involves a call to the relevant interceptor (Intercepter) before and after the action is invoked.
8. Once the action is executed, Actioninvocation is responsible for finding the corresponding return result according to the configuration in the Struts.xml. The return result is usually (but not always, possibly another action chain) a JSP or freemarker template that needs to be represented. The tags inherited in the STRUTS2 framework can be used in the representation process. In this process, it is necessary to involve actionmapper.
2 struts design pattern
MVC mode : Actionservlet is loaded and initialized when the Web application starts. When a user submits a form, a configured Actionform object is created and populated with the corresponding data in the form. Actionservlet determines whether form validation is required based on the settings configured for the Struts-config.xml file, and if it is necessary to choose which action to send the request to, after calling Actionform's validate () validation, if the action does not exist, Actionserv The let will first create the object and then call the action's execute () method. Execute () Gets the data from the Actionform object, completes the business logic, returns a Actionforward object, actionservlet the client request to the JSP component specified by the Actionforward object, The Actionforward object specifies that the JSP generates a dynamic Web page that is returned to the customer.
3 features of the Struts2
Struts2 is a fairly powerful Java Web Open source Framework, an MVC web framework based on the Pojo action. Based on the webwork and xwork frameworks of the year, it inherits its merits and makes considerable improvements.
1,Struts2 based on MVC architecture , frame structure clear, development process at a glance, Developers have a good grasp of the development process.
2, using OGNL for parameter transfer. OGNL provides an easy way to access data from various scopes in the STRUTS2, and you can easily get data from the request,attribute,application,session,parameters. Greatly simplifies the amount of code the developer gets when the data is fetched.
3, a powerful interceptor . Struts2 Interceptors are an action-level aop,struts2 in which many of the features are implemented by interceptors, such as exception handling, file upload, validation, and so on. Interceptors are configurable and reusable, and some common features such as login verification, permission validation, etc. are placed in interceptors to complete some of the more common features in Java Web projects. In my implementation of a Web project, is the use of STRUTS2 interceptor to complete the system of authorization authentication function.
4, easy to test . Struts2 's action is simple Pojo, which makes it easy to write test cases for the STRUTS2 action, greatly facilitating the testing of Java Web projects.
5, easy to expand the plug-in mechanism . Adding extensions in Struts2 is a pleasant and easy task, just put the required jar packages into the Web-inf/lib folder, and make some simple settings in Struts.xml to expand.
6, modular management. Struts2 has taken modularity as the basic idea in architecture, and there are three ways to modularize applications: Splitting configuration information into multiple files, creating new framework features for self-contained application modules as plug-ins, and organizing new features unrelated to specific applications into plugins. To add to multiple apps.
7, global results and declarative exceptions . Add global result for the application and handle the exception in the configuration file so that when the specified exception occurs during processing, you can jump to a specific page.
3 Struts2 Interceptor Principle
Struts2 interceptors are relatively simple to implement. When the request reaches the servletdispatcher of Struts 2, Struts2 looks for the configuration file, instantiates the relative interceptor object based on its configuration, and then strings it into a list, the last one to invoke the interceptor in the list. In fact, the reason we are able to use interceptors so flexibly is due to the use of dynamic agents.
http://blog.csdn.net/csh624366188/article/details/7543342
4 Struts2 Interceptor Type
built-in interceptors : struts2 data verification, internationalization and other functions are implemented in the built-in interceptors. As shown below:
custom interceptors:
1, implement the Interceptor class: Com.opensymphony.xwork2.interceptor.Interceptor.
2. Register a custom Interceptor: you need to register in <interceptors>.
3. The interceptor can be placed in the interceptor stack <interceptor-stack>.
4. Apply the interceptor to the action.
5, you can also set the default interceptor. <default-interceptor-refname= "Myinterceptor"/>
http://blog.csdn.net/csh624366188/article/details/7545677
5 The difference between interceptors and filters
There are many similarities between interceptors and filters, but there is a fundamental difference between the two. The main difference is the following points:
1) Interceptors are based on the Java reflection mechanism, and filters are based on function callbacks.
2) The filter relies on the servlet container, and the interceptor does not depend on the servlet container.
3) The interceptor works only on action requests, and filters can work on almost all requests.
4) The interceptor can access the action context, the object in the value stack, and the filter cannot
5) in the life cycle of the action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized (theinit method ).
6 The role of the STRUTS2 core controller
The role of Strutsprepareandexecutefilter :
Responsible for intercepting all user requests specified by <url-pattern>/*</url-pattern>, which filters the user's request when a user request arrives. By default, if a user requests a path without a suffix or suffix ending with a. Action, the request will be transferred to the STRUTS2 framework, otherwise the STRUTS2 framework will skip processing the request.
You can modify the suffix of the action by using the constant "struts.action.extension", such as:
<constant name= "struts.action.extension" value= "Do"/>
7 Struts2 Get HttpServletRequest, HttpSession, ServletContext objects
It can be obtained by the corresponding static method of Servletactioncontext.
8 Struts2 Ways and benefits of managing action
The STRUTS2 framework uses packages to manage the action, and the role of the package is very similar to the class package in Java. Primarily used to manage a set of action related to a business function. In practical applications, we should place a set of actions related to a business function under the same package.
9 Struts2 Default package Struts-default function
1) The Struts-default package is built by struts, which defines the numerous interceptors and result types inside the struts2, and Struts2 many of the core functions are implemented through these built-in interceptors, such as: Encapsulating request parameters from the request to action, File uploads, data validation, and so on are all implemented through interceptors. The package inherits the Struts-default package to use STRUTS2 to provide us with these features.
2) Struts-default package is defined in struts-default.xml , Struts-default.xml is also the STRUTS2 default profile. STRUTS2 automatically loads the Struts-default.xml file every time.
3) Usually each package should inherit the Struts-default package.
Struts2 validation of the specified method
1) The Validate () method verifies all methods in the action that are identical to the Execute method signature;
2) To verify that the specified method is implemented by overriding the ValidateXxx () method, ValidateXxx () verifies only the method named XXX in the action. Where the first letter of XXX to uppercase;
3) When a data check fails, the Addfielderror () method is called to add the checksum failure information to the fielderrors of the system (in order to use the Addfielderror () method, the action can inherit actionsupport) if the system Fielderrors contains a failure message, STRUTS2 forwards the request to result named input;
4) in the input view, the failure information can be displayed through <s:fielderror/>.
5) First execute validatexxxx ()->validate (), if there is an error, will forward <result name= "input"/> The page specified, if there is no error, will be directly action::execute () Method.
Http://wenku.baidu.com/link?url=i9cPHN8n7qGp0WpK78nqFiEzcbo5hYF1sBscY19jGKBDwcUyrmOgyIbZeGPj1SfeVr3rGHGUk_ 2avisfvjsztlutzykkhdncimzv1mpu0oq
struts2 Default Interceptor
fileUpload: Provide file upload function
i18n: Record user-selected locale
Cookies: Using the configured name,value means that Cookiescheckbox adds a checkbox to automatically process the code, setting the contents of the unchecked checkbox to False, HTML, by default, does not submit a checkbox that is not checked.
chain: Allows the properties of the previous action to be accessed by the latter action, and is now used in conjunction with the chain type of result ().
alias: The request parameter is converted between different requests and the request content is not changed.
the principle and life cycle of Valuestack
1) Valuestack runs through the entire action lifecycle and is stored in the request domain, so the valuestack is the same as the request life cycle. When Struts2 accepts a request, the actioncontext,valuestack,action is created quickly. The action is then stored in the Valuestack, so the instance variable of the action can be accessed by OGNL. When the request comes, the action, Valuestack's life begins, the request ends, and the action, Valuestack's life ends;
2) Action is multiple cases, unlike Servlets, Servelt is a singleton;
3) Each action has a corresponding value stack, the value stack holds the data type is an instance of the action, and the action of the instance variable, the action object is saved at the top of the stack by default;
4) Valuestack is essentially a ArrayList;
5) About contextmap,struts The following maps are pressed into Contextmap,parameters: The map contains the request parameters of the current request; : This map contains all the attributes in the current request object;session: The map contains all the attributes in the current session object;application : The map contains all the properties in the current Application object;attr: The map retrieves a property in the following order: Request,session, Application.
6) When using OGNL to access the contents of the value stack, do not need the # number, while accessing the request, session, application, attr, you need to add the # number;
7) OGNL expression needs to be used in conjunction with struts tags. such as: <s:propertyvalue= "name"/>
8) Reference the OGNL expression in the Struts2 configuration file, referring to the value of the value stack, at which time the "$" is used instead of # or%.
http://blog.csdn.net/wyply115/article/details/8257140
the difference between Actioncontext, ServletContext and PageContext
1)Actioncontext is the context of the current action, and through Actioncontext can obtain references to the action-related objects such as request, session, ServletContext, and so on;
2)ServletContext is a domain object, there is only one servletcontext in a Web application, and the life cycle accompanies the whole Web application;
3)PageContext is the most important built-in object in JSP, can get the application of other domain object through PageContext, and it is a domain object, the scope is only for the current page, at the end of the current page, PageContext destroyed, The life cycle is the smallest of the four domain objects in a JSP.
There are several types of the type of result
Ten species
Dispatcher: The default result type of struts, which forwards control to a resource in the application. You cannot forward control to an external resource, and if you need to direct control weights to an external resource, you should use redirect.
redirect: Redirects the response to another resource (including an external resource).
redirectaction: Redirects the response to another action.
freemarker,velocity,chain,httpheader ,xslt,plaintext,stream
15 The life cycle and working process of interceptors
1) Each interceptor is a Java class that implements the Interceptor interface;
2) init (): This method is called immediately after the Interceptor is created and is called only once during the life cycle of the interceptor. The necessary initialization of related resources can be made in this method;
3) Intercept (actioninvocation invocation): Each time an action request is intercepted, the method is called once;
4) Destroy: This method will be called before the Interceptor is destroyed, and it is only called once during the life cycle of the interceptor;
5) There are 18 interceptors built into the Struts2.
Struts2 File Upload
1 , JSP page:
1) The components of the JSP page upload file: <s:file name= "Upload"/> if you need to upload multiple files at once, you must use multiple file tags, but their names must be the same, namely:name=" xxx" must have the same value;
2) The Enctype property of the form must be set to: Multipart/form-data;
3) The method of the form must be post, because the data submitted by the post is in the body of the message, without the size limit.
2 , the corresponding Action :
1) Add 3 new attributes related to file upload in Action;
2) If you are uploading a single file, the type of the Uploadimage property is Java.io.File, which represents the uploaded file, the second and third attributes are of type String, which represent the file name and file type of the uploaded files, respectively, as follows: JSP page file component name +contenttype,JSP page file The name of the component +filename.
3) If you upload multiple files, you can use an array or List.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Interview-struts2