login.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting pagewelcome.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'welcome.jsp' starting pageerror.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'welcome.jsp' starting pageLoginAction
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