Java Interview-struts2

Source: Internet
Author: User

1 Struts2 Working principle

The processing of a request in the STRUTS2 framework is roughly divided into the following steps:

1. Client Initializes a request to a servlet container (such as Tomcat);

2. This request passes through a series of filters (these filters have an optional filter called Actioncontextcleanup. This filter is very useful for integration of Struts2 and other frameworks, such as: Sitemeshplugin);

3, then Filterdispatcher is called. Filterdispatcher asked Actionmapper to decide if it would be necessary to invoke an action.

4. Suppose Actionmapper decides to call a action,filterdispatcher to give actionproxy the processing of the request.

5, Actionproxy through ConfigurationManager Ask the framework configuration file, find the action class that need to call.

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 completed, Actioninvocation is responsible for finding the corresponding return result based on the configuration in the Struts.xml. The return result is generally (but not always.) May also be 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.

There is a need to involve actionmapper in this process.

2 struts design pattern

MVC mode : Actionservlet is loaded and initialized when the Web application starts.

When the user submits the form. A configured Actionform object is created and populated with the data corresponding to the form, and Actionservlet depends on the configuration of the Struts-config.xml file to determine if form validation is required. Suppose you have to choose which action to send the request to after validate () validation that calls Actionform, assuming that the action does not exist, Actionservlet will first create the object and then call the action's execute () method. Execute () Gets the data from the Actionform object, finishes the business logic, and returns a Actionforward object. The Actionservlet then forwards the client request to the JSP component specified by the Actionforward object, actionforward the specified JSP to generate a Dynamic Web page, which 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. It inherits its merits based on the webwork and xwork frameworks of the year. A considerable improvement was made at the same time.

1,Struts2 based on MVC architecture . The framework is clearly structured and the development process at a glance. Developers have a very good grasp of the development process.

2 , using OGNL to pass the number of parameters.

OGNL provides an easy way to access data from various scopes in Struts2, and you can easily get request,attribute. Application. The data in the session,parameters. Greatly simplifies the amount of code the developer gets when it comes to this data.

3 , a powerful interceptor .

The Struts2 Interceptor is an action-level AOP. Many of the features in STRUTS2 are implemented through interceptors, such as exception handling and file uploads. Verification and so on.

Interceptors are configurable and reusable. Ability to use some common features such as login verification. Permission validation is placed in the interceptor to complete some of the more common features in the Java Web project.

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 a trial example of Struts2 action. The big generosity was the test of the Java Web project.

5, easy to expand the plug-in mechanism .

Adding an extension to Struts2 is a pleasant and relaxing thing to do. Just need to put the required jar package into the Web-inf/lib directory. The extension can be implemented with some simple setup in Struts.xml.

6, modular management. Struts2 has taken modularity as the basic idea in architecture, and has the ability to modularize applications in three ways: splitting configuration information into multiple files creates a new framework feature for the plug-in from the included application module. New features that are not relevant to a particular application are organized into plug-ins. To add to multiple applications.

7, global results and declarative exceptions . adds global result to the application. And the exception is handled in the configuration file so that when a specified exception occurs during processing, it is possible to 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 will look for the configuration file. The relative interceptor object is instantiated based on its configuration and then strung into a list. The last one to invoke the interceptor in the list.

In fact, the reason why we can 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.

For example, see the following:


define the Interceptor yourself:

1, implement the Interceptor class: Com.opensymphony.xwork2.interceptor.Interceptor.

2, register their own definition of interceptors: need to be registered in <interceptors>.

3, can put the interceptor into the interceptor stack <interceptor-stack>.

4. Apply the interceptor to the action.

5, 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 a lot of similarities between interceptors and filters. But there is a fundamental difference between the two. The main differences are the following points:

1) Interceptors are based on the Java reflection mechanism. The filter is based on function callbacks.

2) The filter relies on the servlet container, and the interceptor does not depend on the servlet container.

3) Interceptors can only work on action requests, and filters can work on nearly all requests.

4) The interceptor is able to access the action context and 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> when the user requests arrive. The filter filters the user's request.

By default. Suppose the user requests a path with no suffix or suffix ending with a. Action, and the request is transferred to the STRUTS2 framework, otherwise the STRUTS2 framework will skip processing the request.

The ability to change the suffix of the action through 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 within the struts2, while Struts2 's many core functions are implemented through these built-in interceptors, such as: Encapsulating request parameters from the request to the action, File uploads, data validation, and so on are all implemented through interceptors.

When the package inherits the Struts-default package talents use STRUTS2 to provide us with these features.

2) The Struts-default package is defined in the struts-default.xml . Struts-default.xml is also the STRUTS2 default profile. STRUTS2 automatically loads the Struts-default.xml file each time.

3) Usually each package should inherit the Struts-default package.

Struts2 validation of the specified method

1) The Validate () method verifies the same method as all the Execute method signatures in the action;

2) To verify that the specified method is implemented by overriding the ValidateXxx () method, ValidateXxx () simply verifies the method named XXX in the action. The first letter of XXX should be capitalized;

3) When a data check fails, the Addfielderror () method is called to add a checksum failure message to the system's fielderrors (in order to use the Addfielderror () method, the action can inherit actionsupport). Assume that the fielderrors of the system includes failure information. Struts2 will forward the request to result named input;

4) You can display the failure information through <s:fielderror/> in the input view.

5) First run validatexxxx ()->validate (), assuming an error occurred. The page specified by the <result name= "input"/> is forwarded, assuming that there is no error, the Action::execute () method is directly performed.

Http://wenku.baidu.com/link?

Url=i9cphn8n7qgp0wpk78nqfiezcbo5hyf1sbscy19jgkbdwcuyrmogyibzegpj1sfevr3rghguk_ 2avisfvjsztlutzykkhdncimzv1mpu0oq

struts2 Default Interceptor

fileUpload: Provide file upload function

i18n: Record user-selected locale

Cookies: Use the configured Name,value to refer to Cookiescheckbox adding a checkbox to actively 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 interviewed by the latter action, and is now used in conjunction with the chain type of result ().

alias: Convert request parameters between different requests to the same name, request content unchanged

the principle and life cycle of Valuestack

1) Valuestack runs through the entire action lifecycle and is saved in the request domain. So the valuestack is the same as the life cycle of the request.

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 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 an instance variable in the action, the action object is saved to the top of the stack by default;

4) Valuestack is essentially a ArrayList.

5) About contextmap,struts The following maps are pressed into the contextmap. parameters: The map includes the request parameters for the current request. Request: This map includes all the attributes in the current request object;session: The map includes all the attributes in the current session object;application : This map includes all the properties in the current Application object. attr: The map retrieves a property in the following order, for example: Request,session, Application.

6) When using OGNL to access the contents of the value stack. There is no need for the # number. When you visit request, session, application, attr, you need to add the # number;

7) OGNL expression needs to be used in conjunction with the Struts label talent. such as: <s:propertyvalue= "name"/>

8) Refer to 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, through which 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. The life cycle accompanies the entire Web application;

3)PageContext is the most important built-in object in the JSP. Can get the application of other domain objects through PageContext, at the same time it is a domain object, the scope is only for the current page, 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 for struts. Forward control to a resource in the application.

You cannot forward control to an external resource. If you need to direct control weights to an external resource, you should use redirect.

redirect: Redirect The response to a resource (containing an external resource).

redirectaction: Redirect The response to an 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. It is possible to initialize the relevant resources in the method;

3) Intercept (actioninvocation invocation): Every action request is intercepted, the method is called once.

4) Destroy: This method will be called before the Interceptor is destroyed. It is also called only 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"/>. Suppose you need to upload multiple files at once. You must use multiple file tags, but their names must be the same. That is: The value ofname="xxx" must be the same;

2) The Enctype property of the form must be set to: Multipart/form-data;

3) The method of the form must be post, since the data submitted by the post is in the body of the message. Without a limit of size.

2 , the corresponding Action :

1) in Action, add 3 new attributes related to file upload;

2) Assuming that you are uploading a single file, the type of the Uploadimage property is Java.io.File. It represents the file being uploaded, the type of the second and third attributes is String. They represent the file name and file type of the uploaded file, each of which is: The name of theJSP page file component +contenttype, The name of the JSP page file component is +filename.

3) If you upload multiple files, you can use arrays or lists.

Java Interview-struts2

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.