为Action参数注入值,主要使用在的场景为,Action的一个参数的值不是固定的是可以改变的,所以不能直接写在Action中,可以通过Struts配置的方式将值配置到Struts中,然后通过注入的方式将值注入到Action中,下面是代码:
public class HelloAction { public String resultParam; public String sayHello(){ return "message"; } public String getResultParam() { return resultParam; } public void setResultParam(String resultParam) { this.resultParam = resultParam; } }
struts-config代码:
<package name="hello" namespace="/hello" extends="struts-default"> <action name="helloAction_*" class="cn.actions.HelloAction" method="{1}"> <param name="resultParam">我的名字是:</param> <result name="message">/WEB-INF/pages/message.jsp</result> </action> </package>
message.jsp代码:
<body> ${resultParam} 杰克 </body>
访问地址:http://localhost:9000/Struts2/hello/helloAction_sayHello.do