(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