struts2 国际化 cookie保存语言, 下次访问时显示当前设置的语言
最近在看struts2的国际化, i18n拦截器代码很简单, 具体是根据访问参数和session方式来控制语言, 其实每次都更改语言还是很麻烦的, 特别做了一个Cookie保存当前语言设置, 仅仅一个例子供大家参考, 修改i18n的拦截器代码.
流程处理: 首先判断parameters里面有没有语言选择参数, 有则取出放到cookie, 如果没有则从cookie中取, 放到parameters, 这样就可以实现本地保存, 最终和i18n的拦截器互动, 即使以后struts改了, 我们也能灵活处理; 当然也可以完全从新实现i18n的拦截器功能, 但是那样我觉得后期如果struts i18n方式一变就有点被动.
代码如下:
最近在看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
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
struts 配置
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>
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>