Struts2 Study Notes (5): injection value of the Action attribute and request processed by the specified Struts 2

Source: Internet
Author: User

Struts2 provides the dependency injection function for the attributes in the Action. In the struts2 configuration file, we can easily inject values for the attributes in the Action. Note: The property must provide the setter method.


 1 public class HelloWorldAction{
2 private String savePath;
4 public String getSavePath() {
5 return savePath;
6 }
7 public void setSavePath(String savePath) {
8 this.savePath = savePath;
9 }
10 ......
11 }
<package name="jim" namespace="/test" extends="struts-default">
<action name="helloworld" class="com.jim.action.HelloWorldAction" >
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
The preceding statement uses the <param> node to inject "/images" to the savePath attribute of the action"


Previously we used the. action suffix to access the Action by default. In fact, the default suffix can be modified through the constant "struts. action. extension". For example, we can configure Struts 2 to process only the request paths suffixed with. do:


<?xml version="1.0" encoding="UTF-8"?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<constant name="struts.action.extension" value="do"/>
If you need to specify multiple request suffixes, multiple suffixes are separated by commas. For example: <constant name = "struts. action. extension "value =" do, go "/> constants can be stored in struts. xml or struts. properties configuration. We recommend that you use struts. in xml, the two configuration methods are as follows: In Struts. xml Configuration constant in the file<Struts> <constant name = "struts. action. extension" value = "do"/> </struts> In Struts. properties ConstantStruts. action. extension = do because constants can be defined in the following configuration files, so we need to understand the order in which struts2 loads constants: If the struts-default.xmlstruts-plugin.xmlstruts.xmlstruts.propertiesweb.xml configures the same constant in multiple files, the constant value configured in the next file overwrites the constant value configured in the previous file. common constants <! -- Specify the default sequence set, acting on the setCharacterEncoding method of HttpServletRequest and the output of freemarker and velocity --> <constant name = "struts. i18n. encoding" value = "UTF-8"/> <! -- This attribute specifies the request suffix to be processed by Struts 2. The default value of this attribute is action, that is, all requests matching *. action are processed by Struts2. If you need to specify multiple request suffixes, multiple suffixes are separated by commas. --> <Constant name = "struts. action. extension" value = "do"/> <! -- Sets whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it during development --> <constant name = "struts. serve. static. browserCache "value =" false "/> <! -- When the struts configuration file is modified, whether the system automatically reloads the file. The default value is false (used in the production environment). It is best to open the file during development --> <constant name = "struts. configuration. xml. reload "value =" true "/> <! -- Used in development mode to print more detailed error information --> <constant name = "struts. devMode" value = "true"/> <! -- Default view topic --> <constant name = "struts. ui. theme" value = "simple"/> <! -When integrating with spring, specify that spring is responsible for creating action objects --> <constant name = "struts. objectFactory" value = "spring"/> <! -This attribute sets whether Struts 2 supports dynamic method calls. The default value of this attribute is true. To disable dynamic method calling, set this attribute to false. --> <Constant name = "struts. enable. DynamicMethodInvocation" value = "false"/> <! -- Size limit of uploaded files --> <constant name = "struts. multipart. maxSize" value = "10701096"/>

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: 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.