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;
  	}
       ......
}

好處

  • 上面通過節點爲action的savePath屬性注入“/resource”,可以再hello.jsp頁面獲取/resource
  • 爲action注入屬性值應用於經常換的變量,這樣不用更換源代碼。
  • 比如該變量爲使用該軟件公司的名稱

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章