"" Struts2 Summary (2)

Source: Internet
Author: User

1. For the package in struts. xml:
A. When <package abstract = "true"> is set, it indicates that there is no action configuration in the package, and only one basic public component is defined.
B. The namespace of the package can separate different request paths and be used in many projects. When a request is received,
If no request address exists in the current namespace, the corresponding address will be searched for in the default namespace.

2. modeldriven ),Encapsulate each parameter in an action into a JavaBean (similar
Actionform ). You must implement the modeldriven <t> interface and the GetModel method. After an action implements the modendriven interface,
This action will be intercepted by modendriveninterceptor and set relevant parameters.

3. prevent repeated submission of forms:Set the <s: token name = "***"> </S: token> tag in the JSP form. And in the corresponding action
Set the token interceptor and the returned result named invalid. Token. The key of the error message is struts. Message. invalid. Token.
Configure the key to be internationalized.

4. Coupling between action and servlet container in struts2.There are three main methods:
A. Implement the servletrequestaware or servletresponseaware interfaces. And provides the setting method that you are familiar with the request or response.
B. Use actioncontext (but the response object cannot be obtained ). This method facilitates unit testing.
C. Use servletactioncontext. Servletactioncontext is a subclass of actioncontext.
The first choice is actioncontext, followed by servletactioncontext.

5. integrate multiple struts configuration files,Use the include element in the struts2 configuration file to include other configuration files. Used for multi-module development.

6.You can set initialization parameters for the core filter filterdispatcher of struts2 in Web. XML (Not recommended).

7. dynamic method call (dynamicmethodinvocation ).
A. Specify in action configuration: <action name = "×××" method = ">
B. Specify <s: Form Action = "method! Actionname ">
C. use wildcards: <action name = "* login" class = "com. Action. loginaction" method = "{1}">,
If the action URL is hellologin, The loginaction hello is called to process the service. This method simplifies the configuration,
MakeProgramThis method is not recommended.

8. Result type of struts2.In the struts2-core package, there are several result types that have been configured for struts2.
The default type is dispatcher. In addition, redirectaction, chain, freemaker, httpheader, redirect,
Redirectaction, stream, velocity, XSLT, and plaintext.

9. Integration of struts2 and spring. Import the struts2-spring-plugin package, set the spring listener in Web. XML,
The APIS corresponding to the spring listener are: Org. springframework. Web. Context. contextloaderlistener .
The struts2-spring-plugin package for us to set struts2's object factory to spring's IOC container, its Code Is:
<Struts>
<Bean type = "com. opensymphony. xwork2.objectfactory" name = "Spring" class = "org. Apache. struts2.spring. strutsspringobjectfactory"/>
<! -- Make the spring object factory the automatic default -->
<Constant name = "struts. objectfactory" value = "Spring"/>
<Package name = "Spring-Default">
<Interceptors>
<Interceptor name = "autowiring" class = "com. opensymphony. xwork2.spring. Interceptor. actionautowiringinterceptor"/>
<Interceptor name = "sessionautowiring" class = "org. Apache. struts2.spring. Interceptor. sessioncontextautowiringinterceptor"/>
</Interceptors>
</Package>
</Struts>
Obviously, locate struts. objectfactory as org. Apache. struts2.spring. strutsspringobjectfactory.
The rest of the work is handed over to the Spring IoC container.
In addition, when we need to add spring configuration files, we need to set them in Web. xml. Contextconfiglocation Parameters. The Code is as follows:
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> Other spring configuration file names, separated by commas </param-value>
</Context-param>

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.