您的位置:首页 > 编程语言 > Java开发

Struts2 二、为Action的参数注入值

2016-12-01 00:00 363 查看
为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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: