目录:
首页 Register.jsp :
<body>
<center>
<s:form id="id" action="Register">
<s:textfield name="username" key="username"/>
<s:password name="password" key="password"/>
<s:password name="confirmpassword" key="confirmpassword"/>
<s:textfield name="email" key="email"/>
<s:textfield name="birth" key="birth"/>
<s:submit name="submit" key="submit"/>
<s:reset name="reset" key="reset"/>
</s:form>
</center>
</body>
提交到action-----Register:
package com.zhbit.action;
import java.sql.*;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
import com.zhbit.DB.DBCon;
public class RegAction extends ActionSupport {
private String username;
private String password;
private String confirmpassword;
private String email;
private Date birth;
public RegAction() {};
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 getConfirmpassword() {
return confirmpassword;
}
public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String execute() throws Exception {
//连接数据库,将表单数据存入数据库中
Connection conn;
PreparedStatement pstmt;
String sql = "insert into user values(?,?,?,?)";
conn = DBCon.getConn();
pstmt = DBCon.getPStmt(conn, sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setString(3, email);
// 用Timestamp转换为mysql时间
pstmt.setTimestamp(4, new Timestamp(birth.getTime()));
pstmt.executeUpdate();
return SUCCESS;
}
}
struts.properties:
struts.custom.i18n.resources=globalMessages
globalMessages_en_US.properties: (以key-value形式存入)
username=UserName
password=Password
confirmpassword=Password
email=Email
birth=Birthday
submit=Regist
reset=Reset
title=Struts2_Register
head=SystemRegistration
globalMessages_zh_CN.properties:
eclipse(可能要装插件,主要处理文字转义问题)转换后,本质为:
username=\u7528\u6237\u540D
confirmpassword=\u5BC6\u7801
submit=\u6CE8\u518C
reset=\u91CD\u7F6E
email=Email
birth=\u751F\u65E5
password=\u5BC6\u7801
title=Struts2_\u6CE8\u518C
head=\u7CFB\u7EDF\u6CE8\u518C
注意:
资源文件位置 /src/***
这样就可以实现,一个浏览页面,多国语言的实现了!