java 国际化 demo_国际化---demo1---bai

login.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'index.jsp' starting page

welcome.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'welcome.jsp' starting page

error.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'welcome.jsp' starting page

LoginAction

package com.etc.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport

{

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String execute()

{

//java代码中的国际化

System.out.println(this.getText("username"));

if("123".equals(password)&&"admin".equals(username))

return "success";

return "fail";

}

}

LanguageAction

package com.etc.action;

import java.util.Locale;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LanguageAction extends ActionSupport

{

//转化成中文

public String tochn()

{

Locale loc = new Locale("zh","CN");

//修改当前action上下文的语言配置

ActionContext.getContext().setLocale(loc);

//通过session配置,让新的语言对本会话生效

ServletActionContext.getRequest().getSession()

.setAttribute("WW_TRANS_I18N_LOCALE",loc);

return "success";

}

//转化成英文

public String toeng()

{

Locale loc = new Locale("en","US");

//修改当前action上下文的语言配置

ActionContext.getContext().setLocale(loc);

//通过session配置,让新的语言对本会话生效

ServletActionContext.getRequest().getSession()

.setAttribute("WW_TRANS_I18N_LOCALE",loc);

return "success";

}

}

/welcome.jsp

/error.jsp

/login.jsp

mess_en_US.properties 英文文件 文件命名一定要注意:xxx_en_US.properties

tip=Please input your name and password

username=User Name

password=Your password

submit=Login

reset=Reset

sucinfo=Login ok.You are Welcome.

failinfo=Login fail.

tochn=Chinese Ver.

toeng=English Ver.

mess_zh_CN.properties 中文文件

tip=\u8BF7\u8F93\u5165\u7528\u6237\u540D\u548C\u5BC6\u7801

username=\u7528\u6237\u540D

password=\u5BC6\u7801

submit=\u767B\u5F55

reset=\u91CD\u7F6E

sucinfo=\u767B\u5F55\u6210\u529F\uFF01\u6B22\u8FCE\u4F7F\u7528\u3002

failinfo=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF

tochn=\u4E2D\u6587\u7248

toeng=\u82F1\u6587\u7248

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值