首页 > 代码库 > struts2_4_为Action属性注入值

struts2_4_为Action属性注入值

Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件里,能够为Action中的属性注入值,属性必须提供setter方法。

1employeeAction类:

public class employeeAction {
	private String savePath;

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String execute() {
		return "success";
	}
}

2)struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="package" namespace="/test" 
  extends="struts-default">
		<action name="empl" class="struts.employeeAction" 
  method="execute">
  <!-- 为要注入值得属性注入值-->
			<param name="savePath">zhuRu</param>
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>

3)index.jsp文件:

<body>
 	${savePath }
</body>




struts2_4_为Action属性注入值