struts2 国际化 cookie保存语言, 下次访问时显示当前设置的语言
最近在看struts2的国际化, i18n拦截器代码很简单, 具体是根据访问参数和session方式来控制语言, 其实每次都更改语言还是很麻烦的, 特别做了一个Cookie保存当前语言设置, 仅仅一个例子供大家参考, 修改i18n的拦截器代码.
流程处理: 首先判断parameters里面有没有语言选择参数, 有则取出放到cookie, 如果没有则从cookie中取, 放到parameters, 这样就可以实现本地保存, 最终和i18n的拦截器互动, 即使以后struts改了, 我们也能灵活处理; 当然也可以完全从新实现i18n的拦截器功能, 但是那样我觉得后期如果struts i18n方式一变就有点被动.
代码如下:
1package sh.mgr.ui.interceptor;
2
3import java.util.Locale;
4import java.util.Map;
5
6import javax.servlet.http.Cookie;
7import javax.servlet.http.HttpServletRequest;
8import javax.servlet.http.HttpServletResponse;
9
10import org.apache.struts2.ServletActionContext;
11
12
13import com.opensymphony.xwork2.ActionInvocation;
14import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
15import com.opensymphony.xwork2.interceptor.I18nInterceptor;
16import com.opensymphony.xwork2.util.LocalizedTextUtil;
17import com.opensymphony.xwork2.util.logging.Logger;
18import com.opensymphony.xwork2.util.logging.LoggerFactory;
19
20public class I18nSh extends AbstractInterceptor {
21 private static final long serialVersionUID = -1419979008563298812L;
22
23 protected static final Logger LOG = LoggerFactory.getLogger(I18nInterceptor.class);
24
25 public static final String DEFAULT_SESSION_ATTRIBUTE = "WW_TRANS_I18N_LOCALE";
26 public static final String DEFAULT_PARAMETER = "request_locale";
27
28 protected String parameterName = DEFAULT_PARAMETER;
29 protected String attributeName = DEFAULT_SESSION_ATTRIBUTE;
30
31 protected static final String LAN_COOKIE_NAME = "lan";
32
33 public I18nSh() {
34 if (LOG.isDebugEnabled()) {
35 LOG.debug("new I18nInterceptor()");
36 }
37 }
38
39 public void setParameterName(String parameterName) {
40 this.parameterName = parameterName;
41 }
42
43 public void setAttributeName(String attributeName) {
44 this.attributeName = attributeName;
45 }
46
47 @Override
48 public String intercept(ActionInvocation invocation) throws Exception {
49 if (LOG.isDebugEnabled()) {
50 LOG.debug("intercept '"
51 + invocation.getProxy().getNamespace() + "/"
52 + invocation.getProxy().getActionName() + "' { ");
53 }
54
55
56 //get requested locale
57 Map<String, Object> params = invocation.getInvocationContext().getParameters();
58 Object requested_locale = params.get(parameterName);
59 if (requested_locale != null && requested_locale.getClass().isArray()
60 && ((Object[]) requested_locale).length == 1) {
61 requested_locale = ((Object[]) requested_locale)[0];
62
63 if (LOG.isDebugEnabled()) {
64 LOG.debug("requested_locale=" + requested_locale);
65 }
66 }
67
68 //save it in session
69 Map<String, Object> session = invocation.getInvocationContext().getSession();
70
71
72 if (session != null) {
73 synchronized (session) {
74 Cookie cookie = getLocaleCookie();
75 if (requested_locale != null) {
76 Locale locale = (requested_locale instanceof Locale) ?
77 (Locale) requested_locale : LocalizedTextUtil.localeFromString(requested_locale.toString(), null);
78 if (LOG.isDebugEnabled()) {
79 LOG.debug("put request local to cookie=" + locale);
80 }
81 if (locale != null) {
82 this.setLocaleCookie(cookie, requested_locale.toString());
83 }
84 }else if(cookie!=null){
85 requested_locale = cookie.getValue();
86 if (LOG.isDebugEnabled()) {
87 LOG.debug("put local to request_params=" + requested_locale);
88 }
89 params.put(parameterName, requested_locale);
90 }
91 }
92 }
93
94 return invocation.invoke();
95 }
96
97 private Cookie getLocaleCookie(){
98 HttpServletRequest request = ServletActionContext.getRequest();
99 Cookie cookies[] = request.getCookies();
100 if (cookies != null) {
101 for (int a=0; a< cookies.length; a++) {
102 if(cookies[a].getName().equals(LAN_COOKIE_NAME)){
103 return cookies[a];
104 }
105 }
106 }
107 return null;
108 }
109
110 private void setLocaleCookie(Cookie cookie, String lan){
111 HttpServletResponse response = ServletActionContext.getResponse();
112 if(cookie==null){
113 cookie = new Cookie(LAN_COOKIE_NAME, lan);
114 }else{
115 cookie.setValue(lan);
116 }
117 cookie.setMaxAge(Integer.MAX_VALUE);
118 cookie.setPath("/");
119 response.addCookie(cookie);
120
121
122 }
123}
124
1<package name="myDefPkg" extends="struts-default" abstract="true">
2 <interceptors>
3 <interceptor name="i18nSh" class="sh.mgr.ui.interceptor.I18nSh"></interceptor>
4 <interceptor-stack name="myStack">
5 <interceptor-ref name="i18nSh"></interceptor-ref>
6 <interceptor-ref name="defaultStack"></interceptor-ref>
7 </interceptor-stack>
8 </interceptors>
9 <default-interceptor-ref name="myStack" />
10</package>
11<package name="account" extends="myDefPkg">
12 ..
13</package>
转载于:https://www.cnblogs.com/yinpengxiang/archive/2009/10/28/1591225.html
struts2 国际化 cookie保存语言, 下次访问时显示当前设置的语言相关推荐
- vue项目出现此地址,但是访问时显示,嗯… 无法访问此页面localhost 已拒绝连接
vue项目出现此地址,但是访问时显示,嗯- 无法访问此页面localhost 已拒绝连接. 出现原因:端口号8081被占用 解决办法: 1.先查看端口号,win+R打开命令提示符,输入netstat ...
- c语言程序执行时无法输入字符串,C语言程序设计中键盘输入数据的方法分析
1. 引言 C语言程序设计是一门实践性很强的语言课程,在程序设计过程中经常需要从键盘输入所需要的数据.C语言没有专门的输入语句,可以调用C语言编译系统提供的函数库中的库函数来实现.有些程序虽然调试成功 ...
- c语言中调试时go的作用,C语言调用GO
C语言调用GO 最近工作中遇到需要在c语言里面调用go语言的需求,总结了一下,下面代码里面的每一个注释都很有用,闲话不多说,直接上代码~ 示例 GO代码: package main // 这个文件一定 ...
- 电脑保存或者另存为文件时显示文件名无效
如图所示,前几天我卸载了360然后昨天早上隔离了防火墙提示的一个危险文件,到下午想保存东西的时候就这样了,网上搜索发现有人也遇到过这种问题,尝试各种方法无果,最后安装了一个杀毒软件好了,我装的是火绒室 ...
- nginx服务器,访问时显示目录,不直接显示index.php
一.效果 二.解决方案 修改网站配置文件,添加如下代码: autoindex on; autoindex_exact_size off; autoindex_localtime on; 修改后的网站配 ...
- C语言字符集中可显示字符,C++_C语言转义字符实例详解,在字符集中,有一类字符具有 - phpStudy...
C语言转义字符实例详解 在字符集中,有一类字符具有这样的特性:当从键盘上输入这个字符时,显示器上就可以显示这个字符,即输入什么就显示什么.这类字符称为可显示字符,如a.b.c.$.+和空格符等都是可显 ...
- c语言结构体菜单显示框架,请教c语言结构体嵌套问题。field `atItem' has incomplete type...
//菜单项结构体 typedef struct { BYTE aucItemName[20]; //菜单项的内容 WORD32 dwItem ...
- java安装jdk时显示系统管理员设置了系统策略,禁止进行此安装
Win+R 弹出 运行框 输入gpedit.msc命令 与我相应配置 计算机配置 用户 配置
- Java Web —— Session 和 cookie 保存登录信息
session 与 cookie cookie 与 session 应用于互联网中的一项基本技术--会话(客户端与服务端的交互)跟踪技术,用来跟踪用户的整个会话.简单来说,cookie 是通过在客户端 ...
最新文章
- ue4 开发动作游戏_【图片】第三人称动作游戏开发日志【虚幻4吧】_百度贴吧
- (转载)机器学习知识点(十二)坐标下降法(Coordinate descent)
- Connection to node 0 (/192.168.204.131:9092) could not be established
- EE Servlet 3:在Servlet中生成HTML输出
- Django二次开发对接FastDFS
- Java代码精简之道
- 淘宝H5移动端解决方案
- “摔杯一怒为俞渝” 当当创始人李国庆:蓄谋已久的阴谋 不吐不快
- mysql5.7.14启动教程_mysql5.7.14安装配置方法图文详细教程
- 卵巢鸿蒙不全怎么检查,卵巢黄体功能不足怎么办 这三个处理方法要了解
- 在无锡调试的工作,到了泰安出错了
- Spring中的@Autowired自动装配
- Java开发工具 - IDEA 的使用及简单配置
- 为什么你应该学习编程?
- Android箭头图标移动动画实现
- python zxing 识别条码_Python zxing 库解析(条形码二维码识别)
- 关于书面辞职报告和试用期离职
- 协议僵化 or 协议僵化
- Android 7.0 Vold工作流程
- 程序员创业的方向选择