概述

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

配置

<action name="helloworld" class="com.liuyong666.action.HelloWorldAction">
    <param name="savePath">/resource</param>
    <result name="success">/WEB-INF/page/hello.jsp</result>
</action>

对应类中的变化

public class HelloWorldAction{
      private String savePath;

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

好处

  • 上面通过<param>节点为action的savePath属性注入“/resource”,可以再hello.jsp页面获取/resource
  • 为action注入属性值应用于经常换的变量,这样不用更换源代码。
  • 比如该变量为使用该软件公司的名称


蜗牛互联网
354 声望19 粉丝