[Struts2 Study Notes (3)] Inject values into the Action attributes

Source: Internet
Author: User


(1) Why is there property injection?

This method is applicable when some attributes are not suitable for fixed writes. For some frequently used classes or methods, many classes will be used. Using Attribute injection will save more effort, in addition, this attribute can be defined in advance during design.

(2) What needs to use property injection?

A few practical examples are provided: log function, unified transaction commit, and acquisition of additional information of the current user.


(3) 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.


(4) specific instance

The following uses the <param> node to inject "/images" to the savepath attribute of action"


public class HelloWorldAction{private String savePath;public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}}

<package name="itcast" namespace="/test" extends="struts-default"><action name="helloworld" class="cn.itcast.action.HelloWorldAction" ><param name="savePath">/images</param><result name="success">/WEB-INF/page/hello.jsp</result></action></package>



[Struts2 Study Notes (3)] Inject values into the Action attributes

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.