一、Action配置中的各項默認值
<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.csdn.action.HelloWorldAction" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
1>如果沒有為action指定class,默認是ActionSupport。
2>如果沒有為action指定method,默認執行action中的execute() 方法。
3>如果沒有指定result的name屬性,默認值為success。
二、Action中result的各種轉發類型
<action name="helloworld" class="cn.csdn.action.HelloWorldAction">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
result配置類似于struts1中的forward,但struts2中提供了多種結果類型,常用的類型有: dispatcher(默認值)、 redirect 、 redirectAction 、 plainText。
下面是redirectAction 結果類型的例子,如果重定向的action中同一個包下:
<result type="redirectAction">helloworld</result>
如果重定向的action在別的命名空間下:
<result type="redirectAction">
<param name="actionName">helloworld</param>
<param name="namespace">/test</param>
</result>
plaintext:顯示原始文件內容,例如:當我們需要原樣顯示jsp文件源代碼 的時候,我們可以使用此類型。
<result name="source" type="plainText ">
<param name="location">/xxx.jsp</param>
<param name="charSet">UTF-8</param><!-- 指定讀取文件的編碼 -->
</result>
在result中還可以使用${屬性名}表達式訪問action中的屬性,表達式里的屬性名對應action中的屬性。如下:
<result type="redirect">view.jsp?id=${id}</result>
三、多個Action共享一個視圖--全局result配置
當多個action中都使用到了相同視圖,這時我們應該把result定義為全局視圖。struts1中提供了全局forward,struts2中也提供了相似功能:
<package ....>
<global-results>
<result name="message">/message.jsp</result>
</global-results>
</package>
四、為Action的屬性注入值
Struts2為Action中的屬性提供了依賴注入功能,在struts2的配置文件中,我們可以很方便地為Action中的屬性注入值。注意:屬性必須提供setter方法。
public class HelloWorldAction{
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
......
}
<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.csdn.action.HelloWorldAction" >
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
上面通過<param>節點為action的savePath屬性注入“/images”
五、指定需要Struts 2處理的請求后綴
前面我們都是默認使用.action后綴訪問Action。其實默認后綴是可以通過常量”struts.action.extension“進行修改的,例如:我們可以配置Struts 2只處理以.do為后綴的請求路徑:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
如果用戶需要指定多個請求后綴,則多個后綴之間以英文逗號(,)隔開。如:
<constant name="struts.action.extension" value="do,go"/>
六、細說常量定義
常量可以在struts.xml或struts.properties中配置,建議在struts.xml中配置,兩種配置方式如下:
在struts.xml文件中配置常量
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
在struts.properties中配置常量
struts.action.extension=do
因為常量可以在下面多個配置文件中進行定義,所以我們需要了解struts2加載常量的搜索順序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多個文件中配置了同一個常量,則后一個文件中配置的常量值會覆蓋前面文件中配置的常量值.
七、常用的常量介紹
<!-- 指定默認編碼集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的輸出 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 該屬性指定需要Struts 2處理的請求后綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
如果用戶需要指定多個請求后綴,則多個后綴之間以英文逗號(,)隔開。 -->
<constant name="struts.action.extension" value="do"/>
<!-- 設置瀏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 當struts的配置文件修改后,系統是否自動重新加載該文件,默認值為false(生產環境下使用),開發階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 開發模式下使用,這樣可以打印出更詳細的錯誤信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默認的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb