struts2教程:6、为Action属性注入值

本文详细介绍了如何在Struts2框架中为Action类的属性进行依赖注入,包括配置步骤和注意事项,适用于经常需要更换变量的情况,避免了频繁修改源代码的繁琐过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Action的属性注入值

Struts2Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。

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>

上面通过<param>节点为actionsavePath属性注入“images”,可以再hello.jsp页面获取images

为action注入属性值应用于经常换的变量,这样不用更换源代码。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值