Struts2 interview questions

Source: Internet
Author: User
Tags i18n

What are the advantages of struts2?
1) in software design, struts2 applications can be independent of servlet APIs and Struts APIs. This design of struts2 is a non-intrusive design;
2) interceptor to implement functions such as parameter interception and injection;
3) type converter, which can convert special request parameters into required types;
4) Multiple presentation layer technologies, such as JSP, freemarker, and velocity;
5) Input Validation of struts2 can verify a specified method;
6) provides international resource file management implementation in the global scope, package scope, and Action Scope.

How does struts2 start?
The struts2 framework is started through a filter, that is, strutsprepareandexecutefilter, which is the core filter of struts2;
The init () method of strutsprepareandexecutefilter reads the default configuration file struts. XML in the class path to complete initialization. After struts2 reads the content of Struts. XML
It can be encapsulated into a JavaBean object and stored in the memory. In the future, each request processing will use data in the memory, instead of reading the Struts. xml file every request.

What is the core controller of the struts2 framework? What does it do?
1) The core controller of the struts2 framework is strutsprepareandexecutefilter.
2) role:
Intercepts all user requests specified by <URL-pattern>/* </url-pattern>. When user requests arrive, the filter filters user requests. By default, if the path requested by the user
The request will be transferred to the struts2 framework for processing without a suffix or suffix ending with. Action. Otherwise, the struts2 framework will skip the processing of the request.
You can use the constant "struts. Action. Extension" to modify the suffix of an action, for example:
<Struts> <constant name = "struts. Action. Extension" value = "do"/> </struts>
If you need to specify multiple request suffixes, multiple suffixes are separated by commas. For example:
<Constant name = "struts. Action. Extension" value = "do, go"/>

What is the loading sequence of struts2 configuration files?
Struts. xml --> struts. Properties
Constants can be stored in struts. XML or struts. properties configuration. If the same constant is configured in multiple files, the constant value configured in the next file overwrites the constant value configured in the previous file.
Role of Struts. xml file: notifies the struts2 framework to load corresponding action resources

How can I modify struts2 constants?
Constants can be configured in struts. xml or struts. properties. The two configuration methods are as follows:
1) Configure constants in the Struts. xml file
<Struts>
<Constant name = "struts. Action. Extension" value = "do"/>
</Struts>
2) Configure constants in struts. properties (the Struts. properties file is placed under SRC ):
Struts. Action. Extension = do
 

How does struts2 access three domain objects: httpservletrequest, httpsession, and servletcontext?
Solution 1:
Httpservletrequest request = servletactioncontext. getrequest ();
Httpservletresponse response = servletactioncontext. getresponse ();
Httpsession session = request. getsession ();
Servletcontext = servletactioncontext. getservletcontext ();
 
Solution 2:
Class implements servletrequestaware, servletresponseaware, sessionaware, servletcontextaware
Note: The Framework automatically imports the corresponding domain object

How does struts2 manage actions? What are the advantages of this management method?
In the struts2 framework, packages are used to manage actions. The role of packages is similar to that of class packages in Java.
It is mainly used to manage a group of actions related to business functions. In practical applications, we should put a group of actions related to business functions under the same package.

What is the role of Struts-default, the default package in struts2?
1) The Struts-default package is built in struts. It defines many interceptor and result types in struts2, and many core functions of struts2 are implemented through these built-in interceptor, such: from request
Encapsulation of request parameters to action, file upload, and data verification is implemented through the interceptor. Only when the package inherits the Struts-default package can we use the features provided by struts2.
2) Struts-default package is defined in the struts-default.xml and the struts-default.xml is also the struts2 default configuration file. Struts2 automatically loads the struts-default.xml file each time.
3) Generally, each package should inherit the Struts-default package,

Struts2 workflow?
User request
--> Strutsprepareandexecutefilter
--> Interceptor
--> Action
--> Result
--> JSP/html
--> Response

What is the URL access path of struts2 action?
The package namespace + action name [+ action suffix] is composed of two parts: package. namespace + action. Name

Can the class attribute not be specified for action in struts. xml? What are the default properties of struts2?
Yes. If no class is specified for action, the default class is com. opensymphony. xwork2.actionsupport.
 
Default properties:
If class actionsupport is not specified for action
If method execute () is not specified for action, note: Return "success" in the execute method of actionsupport ";
If the name success of the result is not specified
If no type dispatcher is specified for the result

By default, each action has a reference of the interceptor, namely: <Interceptor-ref name = "defaultstack">;
Note:
1) Once we explicitly specify an interceptor for an action in this package, the default interceptor does not work, so we need to explicitly introduce the default interceptor;
2) Only one default interceptor can be specified for each package. Each default interceptor is valid only for the current package.

How does struts2 verify the specified method?
1) The Validate () method verifies all the methods in the action that are the same as the execute method signature;
2) To verify that the specified method is implemented by rewriting the validatexxx () method, validatexxx () only verifies the method named XXX in the action. The first letter of XXX must be in uppercase;
3) when a data verification fails, call the addfielderror () method to add verification failure information to the system's fielderrors (to use the addfielderror () method, action can inherit actionsupport ),
If the system's fielderrors contains Failure Information, struts2 will forward the request to the result named input;
4) in the input view, you can use <s: fielderror/> to display the failure information.
5) execute validatexxxx ()-> validate ()-> if an error occurs, the page specified by <result name = "input"/> is forwarded. If no error occurs, the Action: Execute () method is directly executed.
 

By default, can struts2 solve the garbled problem of get and post submission methods?
No. Struts. i18n. Encoding = The UTF-8 property value can only resolve the garbled problem under post submission.

How does struts2 complete file upload?
1. jsp page:
1) components for uploading files on the JSP page: <s: file name = "Upload"/>. to upload multiple files at a time, you must use multiple file tags, however, their names must be the same:
Name = "XXX" must have the same value;
2) The form's enctype attribute must be set to multipart/form-data;
3) The form method must be post, because the data submitted by post is in the message body without the size limit.
2. Corresponding action:
1) add three new attributes related to file upload in action;
2) If you upload a single file, the uploadimage attribute type is Java. io. file, which indicates the file to be uploaded. the type of the second and third attributes is string.
File Name and file type. The definitions are as follows:
JSP page file component name + contenttype,
JSP page file component name + filename
3) If you upload multiple files, you can use an array or list


What is the difference between struts2 interceptor and interceptor stack:
1) The struts2 interceptor implements interception before or after accessing an action method. The struts2 interceptor is pluggable.
2) interceptor Stack: Concatenates the interceptor into a chain in a certain order. when accessing the intercepted method, the interceptors in the struts2 interceptor chain will be in the order defined previously
It is similar to the configuration sequence call of the filter in the web. xml file.
 

What is the life cycle and working process of the interceptor?
1) Each interceptor is a Java class that implements the interceptor interface;
2) Init (): This method is called immediately after the interceptor is created. It is called only once within the lifecycle of the interceptor. related resources can be initialized in this method;
3) intercept (actioninvocation Invocation): each time an action request is intercepted, this method will be called once;
4) destroy: This method will be called before the interceptor is destroyed, and it will only be called once within the lifecycle of the interceptor;
5) struts2 has 18 built-in interceptors.
 
What is the role of the interceptor? What is the difference between interceptor and filter?
1) the interceptor acts on the called action. It provides a mechanism for developers to define the code to be executed before and after an action is executed. The interceptor can only intercept action, indicating that the white point interceptor is actually action
Is executed only before and after the action. The interceptor can abstract a part of the code to improve the original action. At the same time, it can reduce code redundancy and improve the reuse rate.
2) The filter intercepts user requests, and the range is much larger than that of the interceptor.
 

Can you write at least five default interceptors in struts2?
Fileupload provides the file upload function
I18n records the selected locale
Cookies use the configured name and value to indicate cookies.
The checkbox automatically processes code added to the checkbox, and sets the content of the unselected checkbox to false. By default, HTML does not submit the unselected checkbox.
Chain enables the attribute of the previous action to be accessed by the next action. It is now used in combination with the result () of the chain type.
Alias converts Request Parameters in different name parts between different requests, and the request content remains unchanged.
 
 
What result types are included in the type attribute of result?
A total of 10 types:
The default result type of dispatcher struts. Forwarding control to a resource in the application cannot forward control to an external resource. to redirect control to an external resource, use
Redirect result type
Redirect redirects the response to another resource (including one external resource)
Redirectaction redirects the response to another action
 
Freemarker, velocity, chain, httpheader, XSLT, plaintext, stream

How does struts2 convert HTML forms to actions?
1) In struts2, The Params interceptor is responsible for ing request parameters to the action attribute. It is a member of the default defaultstack Interceptor. The Params interceptor can automatically complete the string.
And the basic data type.
2) for conversion of referenced data (except string and Collection), a custom type converter is required;
3) The custom type converter must implement the typeconverter interface or expand some specific implementation of this interface, such as strutstypeconverter, and rewrite the converttostring and convertfromstring methods;
4) two types of custom converters:
> Local:
Create an attribute file: ActionClassName-conversion.properties, which needs to be placed in the same directory as the corresponding action class, actionclassname is the action class name,
The following-conversion. properties is a fixed syntax. The content in the properties file is: property name = full Class Name of the type converter
> Global:
Create a WEB-INF file under the xwork-conversion.properties/classes/directory. The content in the properties file is: type to be converted = full Class Name of the type converter
Note: For conversion of attributes, you only need to write the attribute name. If the conversion is of type, you need to write the full class name.
 

How does the value stack valuestack work and its lifecycle?
1) valuestack runs through the lifecycle of the entire action and is stored in the request domain. Therefore, valuestack and request have the same lifecycle. When struts2 accepts a request, it will quickly create actioncontext,
Valuestack, action. Then, the action is stored in valuestack, so the instance variable of the action can be accessed by ognl. When a request comes, the life of action and valuestack starts, and the request ends,
The life of valuestack ends;
2) action is multi-sample. Unlike servlet, servelt is single-sample;
3) each action has a corresponding value stack. The data type stored in the value stack is the instance of the action and the instance variables in the action. Action objects are stored at the top of the stack by default;
4) valuestack is essentially an arraylist;
5) for contextmap, Struts will push the following mappings into contextmap:
Parameters: the map contains the Request Parameters of the current request.
Request: This map contains all the attributes of the current request object.
Session: This map contains all the attributes of the current session object.
Application: This map contains all attributes of the current application object.
ATTR: This map retrieves an attribute in the following order: request, session, Application

6) when using ognl to access the content of the value stack, # is not required. when accessing request, session, application, and ATTR, # is required;
7) Note: In struts2, ognl expressions must be used with Struts labels. For example: <s: property value = "name"/>
8) reference the ognl expression in the struts2 configuration file and reference the value of the value stack. In this case, use "$" instead of # Or %;

What are the differences between actioncontext, servletcontext, and pagecontext?
1) actioncontext is the context of the current action. You can use actioncontext to obtain references to action-related objects such as request, session, and servletcontext;
2) servletcontext is a domain object. A Web application has only one servletcontext, and its lifecycle is accompanied by the entire web application;
3) pagecontext is the most important built-in object in JSP. You can use pagecontext to obtain applications of other domain objects. It is also a domain object and its scope is only for the current page, when the current page ends, pagecontext is destroyed,
The lifecycle is the smallest of the four JSP domain objects.

How does struts2 prevent repeated submission of user forms?
Step 1: Add <s: token/> to the form.
Step 2: Use the token bar Interceptor to define the invalid. Token result set.

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.