05. struts2中为Action属性注入值

概述

  • 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注入属性值应用于经常换的变量,这样不用更换源代码。
  • 比如该变量为使用该软件公司的名称
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容