posts - 325,  comments - 25,  trackbacks - 0

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中的成员属性,并不一定只用于封装用户的请求参数,也可能用于封装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()方法会在业务方法运行前执行,同时也提供了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实现文件上传限制的两种方式:
    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中配置参数
 <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 长春语林科技 阅读(348) 评论(0)  编辑  收藏 所属分类: struts2

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

 

长春语林科技欢迎您!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜