1.<action/>的result type類型:
<!-- 配置action 返回cancle時(shí),重定向到另一個(gè)Action-->
<result name = "cancle" type="redirect-action">welcom</result>
<!--配置action 返回expired時(shí)進(jìn)入下一個(gè)action 連-->
<result name= "expired" type="chain" >changepassword</result>
2.Action中的成員屬性,并不一定只用于封裝用戶的請(qǐng)求參數(shù),也可能用于封裝Action向下一個(gè)頁面的值,實(shí)際上這些值都被封裝在ValueStack中
我們可以通過
<%
ValueStack vs = (ValueStack) request.getAttribute("struts.valueStack");
Object o = (Object)vs.findValue("key");
%>
來獲取這些值。
3.國際化資源文件的命名方式 basename_語言代碼_國家代碼.properties
如果資源文件被放置到\classes\com\test下,則在struts.xml中配置加包名的引入方式
struts.custom.i18n.resources = com.test.basename_語言代碼_國家代碼.properties
struts2提供了兩種方式來輸出國際化信息:
·<s:text name="messageKey"/>
·<s:property value="%{getText("messageKey")}"/>
4.struts2的ActionSupport類的validate()方法會(huì)在業(yè)務(wù)方法運(yùn)行前執(zhí)行,同時(shí)也提供了getText("messageKey")顯示國化際消息的方法,<s:form >也默認(rèn)提供了輸出錯(cuò)誤消息的功能
public void validate(){
if(null==this.getUserName() || "".equals(this.getUserName()){
addFieldError("userName",getText("user.required"));
}
}
5.struts2聲明式異常處理
<!-- 定義全局result-->
<global-results>
<result name="root">/exception.jsp</result>
</global-results>
<!--定義全局異常映射-->
<global-exception-mappings>
<!--當(dāng)Action拋出Exception異常時(shí),轉(zhuǎn)入root對(duì)應(yīng)的視圖-->
<exception-mapping exception="java.lang.Exception" result="root"/>
</global-exception-mappings>
<!--異常信息輸出-->
在exception.jsp中,輸出異常信息
<s:property value="exception"/> //輸出異常對(duì)象本身
<s:property value="exceptionStack"/> //輸出異常堆棧信息
6.struts2實(shí)現(xiàn)文件上傳限制的兩種方式:
1).在action中增加如下方法
public String filterType(String[] types)
{
String fileType = vo.getXXXContentType();
for (String type : types)
{
if (type.equals(fileType))
{
return null;
}
}
return INPUT;
}
2).在vo中增加private String allowTypes;及set get 方法
3).execute方法中
String filterResult = filterType(getAllowTypes().split(","));
if (filterResult != null)
{
ActionContext.getContext().put("typeError" , "您要上傳的文件類型不正確!");
return filterResult;
}
if(vo.getFile().length()>1024*n){
ActionContext.getContext().put("sizeError" , "您要上傳的文件過大!");
return filterResult;
}
4).上傳頁面中顯示的錯(cuò)誤信息
${requestScope.typeError}
${requestScope.siezeError}
5).struts.xml action中配置參數(shù)
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
7.OGNL 表達(dá)式:struts2中有一個(gè)根對(duì)象 valueStack ,如果需要訪問valueStack中的屬性,要使用$ ,如${bar}
此外,struts2還提供一些命名對(duì)象,這些命名對(duì)象與根對(duì)象無關(guān),需要使用#訪問
命名對(duì)象:
parameters對(duì)象的訪問:#parameters.foo 用于返回HttpServletRequest 的getParameter("foo")的值
request對(duì)象的訪問: #request.['foo'] 或著 #request.foo 用于返回HttpServletRequest 的getAttribute("foo")的值
session對(duì)象的訪問:#session['foo'] 或$session.foo 用于返回HttpSession 的getAttribute("foo")的值
application對(duì)象的訪問:#application['foo'] 或$application.foo 用于返回HttpApplication 的getAttribute("foo")的值
attr 對(duì)象的訪問:
8.在Action被實(shí)例化后,action對(duì)象就被保存到了ValueStack 中,所以不需要使用#就可以訪問Action的屬性
:
posted on 2009-05-22 10:04
長春語林科技 閱讀(373)
評(píng)論(0) 編輯 收藏 所屬分類:
struts2