Struts2 中动态切换国际化

 
Action 写法:
package cn.struts2.nick.util;

import java.util.Locale;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LangAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	String Country;
	String language;
	
	public String execute() throws Exception {
		Locale locale = new Locale(Country,language); 
		ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE", locale); 		
		return super.execute();
	}

	public String getCountry() {
		return Country;
	}

	public void setCountry(String country) {
		Country = country;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
}


struts.xml

 <constant name="struts.custom.i18n.resources" value="struts2"></constant>
  
 <package name="lang" namespace="/" extends="struts-default">
        <action name="lang" class="cn.struts2.nick.util.LangAction" >              
                <result name="success">/index.jsp</result>               
        </action>
    </package>


jsp 写法:

<form action="<%= request.getContextPath() %>/login.action" method="post">   
	  <s:text name="login_userName" />  <input name="userName" size="19" type="text" /><br />
	  <s:text name="login_Password" /> <input name="Password" Size="19" Type="password"/><br />
	   <input type="submit" value="Submit" />
   </form>
   
   <a href="<%= request.getContextPath() %>/lang.action?Country=en&language=US">english</a>
   <a href="<%= request.getContextPath() %>/lang.action?Country=zh&language=CN">chainese</a>


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值