struts2 international English switch

In order to realize the internationalization of the program, we must provide the resources needed for the program files. The basic contents of the resource file is key-value pairs, where the key is to use part of the program, and the value is a part of the program.

Resource files can be named the following three forms:

  1. baseName_language_country.properties
  2. baseName_language.properties
  3. baseName.properties

BaseName which is the base name of the resource file, the user can freely define, and language and country are the same, must be supported by the Java language and country. (Which country linguistic resources can be used to check the official documentation)

Java can not support all the countries and languages, you can get support by getAvailableLocale method Locale class, which returns a Locale array that contains all countries and languages ​​supported.

Create a resource file:

globalMessages_en_US.properties (English)

1

globalMessages_zh_CN.properties (Chinese)

2

Write jsp page, this switching effect in the jsp test English:

<s:i18n name="globalMessages">
  	
    <form id="form1" action="${pageContext.request.contextPath }/LoginAction_login" method="post">
    <div>
        <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><img src="Img/login_1.jpg" width="659" height="104"></td>
        </tr>
      <tr>
        <td width="53%" class="login_p1">
        <div class="login_p2">
        <div class="login_p3">
        <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr>
              <td height="44" align="left" valign="bottom" class="wenzi"><s:text name="username"/></td>
            </tr>
            <tr>
            <s:text name="check"></s:text>:
		  	<a href="LoginAction_login?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
			<a href="LoginAction_login?request_locale=en_US"><s:text name="english"></s:text></a>  
            </tr>
            <tr>
              <td width="87%" height="58" align="left" valign="bottom">                  
                  <input name="userName" type="text">                
              </td>
            </tr>
            <tr>
              <td height="60" align="left" valign="bottom" class="wenzi"><s:text name="password" /></td>
            </tr>
            <tr>
              <td height="58" align="left" valign="bottom">
                 <input name="password" type="password">
              </td>
            </tr>
            <tr>
              <td height="55" align="left" valign="middle"><input type="checkbox" name="checkbox" id="checkbox">
                <s:text name="remember"/></td>
              </tr>
            <tr>
              <td height="77" align="center" valign="bottom">
<!--                    <input id="btnLogin" type="submit" value="登录"> -->
                   <s:submit value="%{getText('loginBtn')}" id="btnLogin"/>
            </td>
            </tr>
          </table>
        </div>
        </div>
        </td>
        </tr>
      <tr>
        <td height="128" align="center"><span class="login_p5">Copyright&copy;版权所有2016江苏美正生物科技有限公司 All Rights Reserved.</span></td>
      </tr>
    </table></td>
  </tr>
</table>
    </div>
    </form>
</s:i18n>

There is a i18n, he is Internationalization (international) abbreviation i18n Why is it?

Internationalization remove head and tail and n i just remaining 18 characters, up posture, the original may be so named

Preparation of action:

public String login(){
		if(model.getUserName()==null){
			return "login";
		}
		User user= userService.login(model);
		if(user !=null){
			ServletActionContext.getRequest().getSession().setMaxInactiveInterval(-1);
			ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
			ServletActionContext.getRequest().setAttribute("menuIds", Arrays.asList(getCurrUserMenuIds().split(", ")));
			ServletActionContext.getRequest().setAttribute("loginUserId", user.getId());
			return "home";
		}else {
			this.addActionError(this.getText("loginError"));
			return "login";
		}
		}

In struts2, there is provided a i18n interceptor, the interceptor Action before performing the method, an automatic search request parameter called request_locale, converted to a Locale object intercepting, placed in a user session entitled "WW_TRANS_I18N_LOCALE" attribute . I18n will be loaded automatically when the program runs, we can use these settings to allow users to automatically select the language of the web page.

Allocation of resources and international interceptors in struts.xml in:

struts.xml:

 <constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
    
    <package name="CurvePlatform" extends="struts-default" namespace="/">
      <!-- i18n语言包拦截器 -->  
      <interceptors>
			<interceptor name="myInter" class="com.wade.CurvePlatform.controller.CheckInterceptor"></interceptor>
	  </interceptors>    
      <action name="LoginAction_*" class="loginAction" method="{1}">
        <result name="home">/index.jsp</result>
        <result name="login">/login.jsp</result> 
        <interceptor-ref name="defaultStack"></interceptor-ref>
		<interceptor-ref name="myInter"></interceptor-ref>           
      </action>

CheckInterceptor.java:

import java.util.Locale;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckInterceptor extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		ActionContext ac = ai.getInvocationContext();
		Map session = ac.getSession();
		Locale locale = (Locale)session.get("WW_TRANS_I18N_LOCALE");
		if(locale==null){
			locale = new Locale("zh","CN");
			session.put("WW_TRANS_I18N_LOCALE",locale);
		}
		return ai.invoke();
	}

}

 

Guess you like

Origin blog.csdn.net/weixin_42656571/article/details/95326570