1.<action/>的result type類型:
<!-- 配置action 返回cancle時,重定向到另一個Action-->
<result name = "cancle" type="redirect-action">welcom</result>
<!--配置action 返回expired時進入下一個action 連-->
<result name= "expired" type="chain" >changepassword</result>
2.Action中的成員屬性,并不一定只用于封裝用戶的請求參數(shù),也可能用于封裝Action向下一個頁面的值,實際上這些值都被封裝在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()方法會在業(yè)務方法運行前執(zhí)行,同時也提供了getText("messageKey")顯示國化際消息的方法,<s:form >也默認提供了輸出錯誤消息的功能
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>
<!--當Action拋出Exception異常時,轉入root對應的視圖-->
<exception-mapping exception="java.lang.Exception" result="root"/>
</global-exception-mappings>
<!--異常信息輸出-->
在exception.jsp中,輸出異常信息
<s:property value="exception"/> //輸出異常對象本身
<s:property value="exceptionStack"/> //輸出異常堆棧信息
6.struts2實現(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).上傳頁面中顯示的錯誤信息
${requestScope.typeError}
${requestScope.siezeError}
5).struts.xml action中配置參數(shù)
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
7.OGNL 表達式:struts2中有一個根對象 valueStack ,如果需要訪問valueStack中的屬性,要使用$ ,如${bar}
此外,struts2還提供一些命名對象,這些命名對象與根對象無關,需要使用#訪問
命名對象:
parameters對象的訪問:#parameters.foo 用于返回HttpServletRequest 的getParameter("foo")的值
request對象的訪問: #request.['foo'] 或著 #request.foo 用于返回HttpServletRequest 的getAttribute("foo")的值
session對象的訪問:#session['foo'] 或$session.foo 用于返回HttpSession 的getAttribute("foo")的值
application對象的訪問:#application['foo'] 或$application.foo 用于返回HttpApplication 的getAttribute("foo")的值
attr 對象的訪問:
8.在Action被實例化后,action對象就被保存到了ValueStack 中,所以不需要使用#就可以訪問Action的屬性
:
posted on 2009-05-22 10:04
長春語林科技 閱讀(365)
評論(0) 編輯 收藏 所屬分類:
struts2