Struts 2 –为Action类配置静态参数

下载它– Struts-Action-Static-ParamExample.zip

在某些情况下,您可能需要为Action类分配一些预定义或静态参数值。

为动作定义了静态参数

在Struts 2中,您可以通过<param>标记在struts.xml文件中对其进行配置,例如,

struts.xml

<struts>

   <constant name="struts.custom.i18n.resources" value="global" />
   <constant name="struts.devMode" value="true" />
	
   <package name="default" namespace="/" extends="struts-default">
	<action name="locale" class="com.mkyong.common.action.LocaleAction">
		<result name="SUCCESS">pages/welcome.jsp</result>
		<param name="EnglishParam">English</param>
    	        <param name="ChineseParam">Chinese</param>
     	        <param name="FranceParam">France</param>
	</action>
   </package>	
</struts>

它将三个预定义的参数值分配给LocaleAction Action类。

从动作获取静态参数

要从struts.xml获取静态参数值,Action类必须实现Parameterizable接口。 它可以使用Map属性JavaBean属性进行访问。

Action的静态参数由staticParams Interceptor控制,该参数包含在默认堆栈“ struts-default.xml”中。

1.地图属性

在Action类初始化期间,staticParams拦截器将通过setParams()方法将预定义参数值的引用传递给Action的类。

//...
import com.opensymphony.xwork2.config.entities.Parameterizable;

public class LocaleAction implements Parameterizable{

	Map<String, String> params;
	//...
	public void setParams(Map<String, String> params) {
		this.params = params;
	}
}

2. JavaBean属性

在Action类初始化期间,如果正确创建了getter和setter方法,则staticParams Interceptor将为与“ param”元素相对应的每个JavaBean属性设置预定义的参数值。

//...
import com.opensymphony.xwork2.config.entities.Parameterizable;

public class LocaleAction implements Parameterizable{

	String englishParam;
	String chineseParam;
	String franceParam;
	
	public String getEnglishParam() {
		return englishParam;
	}

	public void setEnglishParam(String englishParam) {
		this.englishParam = englishParam;
	}

	public String getChineseParam() {
		return chineseParam;
	}

	public void setChineseParam(String chineseParam) {
		this.chineseParam = chineseParam;
	}

	public String getFranceParam() {
		return franceParam;
	}

	public void setFranceParam(String franceParam) {
		this.franceParam = franceParam;
	}
    //...
}

翻译自: https://mkyong.com/struts2/struts-2-configure-static-parameter-for-action-class/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值