posts - 5, comments - 24, trackbacks - 0, articles - 20
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Struts学习笔记

Posted on 2006-09-25 12:26 kook 阅读(336) 评论(0)  编辑  收藏 所属分类: Struts

 

一、 如何获得 ApplicationResources.properties 中的信息


1
、获得默认的资源文件

1 MessageResources messages =  getResources(request);
2 

3 String title = messages.getMessage("hello.jsp.title" );
4 



2
、获得默认的资源文件

1 MessageResources resources =  (MessageResources) request.
2 

3 getAttribute(Globals.MESSAGES_KEY);
4 



3
、获得指定key的资源文件

MessageResources messages  =  getResources(request, " en " );

 

其实方法一本质上就是调用方法二来获得 MessageResources 的实例。

首先获得一个封装了 ApplicationResources.properties 信息

的对象 MessageResources 的实例messages,然后调用getMessage(key);来获得key的值

二、 如何打包发布一个WEB应用

DOC下转到应用的根目录执行jar cvf appName.war *.*

三、 如何在formbean下验证表单

 

 1 ActionErrors errors = new  ActionErrors();
 2 

 3     String array[] = new String[2 ];
 4 

 5     array[0= "xx" ;
 6 

 7     array[1= "hh" ;
 8 

 9     if(name==null || name.length()<1 )
10 

11      {
12 

13        errors.add("name",new ActionMessage("kong",false ));
14 

15     //对应的key为hello,且hello为动态复合消息,array数组用来替换复合消息中的参数
16 
17        errors.add("name",new ActionMessage("hello" ,array));
18 

19      }
20 

21 return  errors;
22 

23 

 

inputJSP中用 <html:errors/> 获取并显示

 

四、 action下处理业务逻辑时返回的错误信息

 1 ActionErrors errors =  new ActionErrors();
 2 

 3
 String userName  = (String)((HelloForm) form).getUserName();
 4 

 5
   
 6 

 7
 String badUserName = "Monster ";
 8 

 9
   
10 

11
  if (userName.equalsIgnoreCase(badUserName)) {
12 

13
      errors.add("username"new ActionError("hello.dont.talk.to.monster ", badUserName ));
14 

15
      saveErrors(request, errors);       // 绑定到request对象中
16
 

17
      return ( new ActionForward(mapping.getInput()));
18 

19
   }
20 


同样在相应的JSP页面中用用<html:errors/>
获取并显示

 

五、 JSP 中如何访问action

1

< A href = " <%=request.getContextPath() %>/hello.do " > Link </ A >

 

2 、自动跳转


   
A_JSP
页面:

< logic:forward name = " gaga " />


B_struts-config.xml
设置:


< global - forwards  >

      <
forward name = " gaga "  path = " /hello.do " ></ forward
>

</
global - forwards >


六、
配置错误页面和异常页面:《精通Struts page_75


七、
如何在Web应用中访问Locale对象


1
、什么是Locale
java.util.Locale 类的实例代表一种特定的语言环境和地区


2
如何获得Locale

²        构造函数

Locale locale  =   new  Locale( " en " , " US ");
Locale locale 
=   new  Locale( " ch " , " CH " );


²    
访问java.util.Locale类的静态常量来获得常用的 Locale 实例

Locale locale  =  Locale.CHINA;     (zh_CN)
Locale locale 
=  Locale.CHINESE;   (zh)

²        通过HttpServletRequest对象来获得客户端的Locale

Locale locale  =  request.getLocale();   // 获得优先使用的Locale
System.out.println(locale);

 

// 获得所有Locale,从优先级高的依次降序排列并打印出来。
Enumeration locales  =  request.getLocales();

while
 (locales.hasMoreElements()) {
      Locale element 
=
 (Locale) locales.nextElement();
      System.out.println(element);
}


²       
通过HttpSession对象来获得Locale

HttpSession session  =  request.getSession();

Locale locale 
=
 (Locale) session.

getAttribute(Globals.LOCALE_KEY);

System.out.println(locale);

 

获得Locale的方法还有很多,其实在Web应用中,很多获得Locale的方法都是不同程度的或者是直接的或者是间接在调用 request.getLocale(); 方法。因此有了 HttpServletRequest ,我们可以做很多事情。


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


网站导航: