论坛首页 Java企业应用论坛

在WebWork2中怎么根据Session中的Locale来读取不同的资源文件?

浏览 4664 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-02-01  
比如有页面中有两个链接:
<a href="LocaleSetter.action?language=zh&country=CN">中文</a>
<a href="LocaleSetter.action?language=en&country=US">English</a>
用户点相应的链接,就在页面上显示中文或英文。
我的想法是这样:一个Action,用来往用户的Session中设置Locale,一个过滤器每次访问JSP文件时往上下文中设置Locale.
代码如下:

public class LocaleSetter implements Action {
	private static Log log = LogFactory.getLog(LocaleSetter.class);;
	
	private String language;
	private String country;
	
	public void setLanguage(String language); {
		this.language = language;
	}
	public String getLanguage(); {
		return language;
	}
	
	public void setCountry(String country); {
		this.country = country;
	}
	public String getCountry(); {
		return country;
	}
	public String execute(); throws Exception {
		Locale userLocale = new Locale(language, country);;
		log.info(userLocale.toString(););;
		ActionContext.getContext();.getSession();.put("user.locale", userLocale);;
		return SUCCESS;
	}
}

public class LocaleFilter implements Filter {
	private static Log log = LogFactory.getLog(LocaleFilter.class);;
	
	public void init(FilterConfig arg0); throws ServletException {
		return;
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain); throws IOException, ServletException {
		Locale userLocale = 
			(Locale);((HttpServletRequest);request);.getSession();.getAttribute("user.locale");;
		if (userLocale != null); {
			log.info(userLocale.toString(););;
			ActionContext.getContext();.setLocale(userLocale);;
		}
		filterChain.doFilter(request, response);;
	}
	
	public void destroy(); {
		return;
	}
}

可是行不通啊 ,请问一下有没有什么比较好的办法?
   发表时间:2005-02-01  
action 类继承 ActionSupport

覆盖 public Locale getLocale() {
                  return 这里你可以选择你所要返回的locale

}
0 请登录后投票
   发表时间:2005-02-02  
oldpig 写道
action 类继承 ActionSupport

覆盖 public Locale getLocale() {
                  return 这里你可以选择你所要返回的locale

}

请问你的意思是我的LocaleSetter继承 ActionSupport,然后Filter就不要了是吗?
0 请登录后投票
   发表时间:2005-02-02  
是啊,
0 请登录后投票
   发表时间:2005-02-03  
刚刚试了,不行啊,第一次可以,但是以后就不行了,验证文件呢,也不行。
如果WebWork标签支持如下的用法就好了:
&lt;ww:i18n name="'message_i18n'" language="zh" country="CN"&gt;
另外如果用了&lt;frame&gt;,那页面就乱了。好象只能重定向单个页面,而不能重定向整个frame.就相当于只能用window.location=""而不能用parent.location=""
0 请登录后投票
   发表时间:2005-03-27  
http://www.opensymphony.com/webwork/wikidocs/Transparent%20web-app%20I18N.html

本人已经测试通过
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics