struts2 國際化,防止刷新重複提交表單與郵箱格式驗證
struts2 國際化與防止刷新重複提交表單
本實例主要是功能是實現國際化,防止刷新得利提交表單,利用struts2的驗證機制驗證字符輸入的合法性,郵箱輸入的正確性:
本實例用兩個頁面(create.jsp,createResult.jsp),一個Action(CreateAction),一個驗證文件(CreateAction-validation.xml),兩個國際化文件(message_en_US.properties,message_zh_CN.properties),還有一個struts.xml(必有的).創建用戶成功之後,顯示剛纔創建的信息,不成功則顯示錯誤提示,錯誤提示使用
了國際化來顯示,輸入合法性就用了用struts2的驗證機制驗證.
如下結構圖,好好對照:
K:/ECLIPSWORKS/STRUTS2TEST
│ .classpath
│ .mymetadata
│ .project
│
├─.myeclipse
├─src
│ │ message_en_US.properties
│ │ message_zh_CN.properties
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ CreateAction-validation.xml
│ CreateAction.java
│
└─WebRoot
│ create.jsp
│ createResult.jsp
│
├─META-INF
│ MANIFEST.MF
│
└─WEB-INF
│ web.xml
│
├─classes
│ │ message_en_US.properties
│ │ message_zh_CN.properties
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ CreateAction-validation.xml
│ CreateAction.class
│
└─lib
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
jcommon-1.0.14.jar
junit.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
struts2-jfreechart-plugin-2.0.11.2.jar
xwork-2.0.5.jar
1.CreateAction.java
// ******************************************************************
package cn.struts2;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class CreateAction extends ActionSupport
{
private String name ;
private String password;
private String repassword;
private Date birthday;
private Date registedDay;
private int age;
private String email;
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return the repassword
*/
public String getRepassword()
{
return repassword;
}
/**
* @param repassword the repassword to set
*/
public void setRepassword(String repassword)
{
this.repassword = repassword;
}
/**
* @return the birthday
*/
public Date getBirthday()
{
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
/**
* @return the registedDay
*/
public Date getRegistedDay()
{
return registedDay;
}
/**
* @param registedDay the registedDay to set
*/
public void setRegistedDay(Date registedDay)
{
this.registedDay = registedDay;
}
/**
* @return the age
*/
public int getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age)
{
this.age = age;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
//****************************************
public String execute()throws Exception
{
return SUCCESS;
}
}
//******************************************************************
2.置全局國際化文件(兩個):
// message_en_US.properties ********************************************
create = Create Users Information
username.invalid = User Name Not Null!
password.invalid.null = Password Not Null!
password.invalid.too.short.or.long = Password should be between 6 and 10
submit = submit
//***********************************************************************
中文國際化
// message_zh_CN.properties ********************************************
create = /u521b/u5efa/u7528/u6237/u4fe1/u606f
username.invalid = /u7528/u6237/u540d/u4e0d/u80fd/u4e3a/u7a7a
password.invalid.null = /u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a
password.invalid.too.short.or.long = /u5bc6/u7801/u957f/u5ea6/u5fc5/u987b/u57286/u523010/u4e4b/u95f4
submit =/u63d0/u4ea4
//***********************************************************************
注意:如(create = /u521b/u5efa/u7528/u6237/u4fe1/u606f)等號右邊的一串亂碼是中文字符對就的ASCII碼值,如果你需要轉換,可以打開你的CMD(開始-->運行-->輸入CMD即可),輸入命令native2ascii,回車,將你的中文字符粘上,再回車就可以看到一串亂碼了.再將其COPY到相應的位置即可.
文件的名字不能亂取,XXX_en_US.properties,XXX_zh_CN.properties,XXX後面的名字是固定的,而前面的XXX是根據你的struts.xml文件中的 <constant name="struts.custom.i18n.resources" value="XXX"></constant>中的XXX而取的.本例的XXX就是message.
3.struts.xml
// ************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="create" class="cn.struts2.CreateAction" >
<result name="success">/createResult.jsp</result>
<result name="input">/create.jsp</result>
<!-- avoid refresh again -->
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token">/create.jsp</result>
</action>
</package>
</struts>
//************************************************************************
4.web.xml
// ****************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
//*************************************************************************
5.JSP文件
(1)
// createResult.jsp ************************************************************************
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> luanmad's JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is luanmad's JSP page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
User Name:${requestScope.name }<br>
Password :${requestScope.password }<br>
Age:<s:property value="age"/><br>
Birthday:<s:property value="birthday"/><br>
RegistedDay:<s:property value="registedDay"/><br>
Email:<s:property value="email"/><br>
</body>
</html>
//***********************************************************************
(2)
// create.jsp *******************************************************************
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>luanmad's JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is luanmad's JSP page ">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="create" method="post">
<!-- 防止刷新重複提交-->
<s:token></s:token>
<s:textfield name="name" label="User Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:textfield name="age" label="Age"></s:textfield>
<s:textfield name="birthday" label="Birthday"></s:textfield>
<s:textfield name="registedDay" label="RegistedDay"></s:textfield>
<s:textfield name="email" label="Email"></s:textfield>
<s:submit key="submit"></s:submit>
<s:reset label="reset"></s:reset>
</s:form>
</body>
</html>
//*******************************************************************
6.驗證文件(注意名字要與你的Action名字一樣,後面再跟固定的(-validation.xml)如(CreateAction-validation.xml)
CreateAction-validation.xml:
// ****************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<!-- 格式的寫法可以參照XWork Validator 1.0.2.dtd -->
<!-- 參數設置參照xwork-2.0.5.jar 下的com.opensymphony.xwork2.validator.validators/default.xml -->
<validators>
<!--驗證誰, 用誰來驗證 -->
<field name="name">
<field-validator type="requiredstring">
<!--requiredstring對應的類的方法裏的參數名trim 如public void setTrim(boolean trim)裏的trim -->
<param name="trim">true</param>
<!-- message key的key內容是I18N裏(即baseName_zh_CN.properties和baseName_en_US.properties中)定義的字段名即等號在邊的名字如(username.invalid = 名字不能爲空)-->
<message key="username.invalid"></message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="password.invalid.null"></message>
</field-validator>
</field>
<field name="password">
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">16</param>
<message key="password.invalid.too.short.or.long"></message>
</field-validator>
</field>
<!-- 以下未做國際化 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message>birthday not null!</message>
</field-validator>
<field-validator type="date">
<param name="min">2000-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message>email format error!</message>
</field-validator>
</field>
</validators>
//*******************************************************************************************
注意導入相應的包.
本實例主要是功能是實現國際化,防止刷新得利提交表單,利用struts2的驗證機制驗證字符輸入的合法性,郵箱輸入的正確性:
本實例用兩個頁面(create.jsp,createResult.jsp),一個Action(CreateAction),一個驗證文件(CreateAction-validation.xml),兩個國際化文件(message_en_US.properties,message_zh_CN.properties),還有一個struts.xml(必有的).創建用戶成功之後,顯示剛纔創建的信息,不成功則顯示錯誤提示,錯誤提示使用
了國際化來顯示,輸入合法性就用了用struts2的驗證機制驗證.
如下結構圖,好好對照:
K:/ECLIPSWORKS/STRUTS2TEST
│ .classpath
│ .mymetadata
│ .project
│
├─.myeclipse
├─src
│ │ message_en_US.properties
│ │ message_zh_CN.properties
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ CreateAction-validation.xml
│ CreateAction.java
│
└─WebRoot
│ create.jsp
│ createResult.jsp
│
├─META-INF
│ MANIFEST.MF
│
└─WEB-INF
│ web.xml
│
├─classes
│ │ message_en_US.properties
│ │ message_zh_CN.properties
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ CreateAction-validation.xml
│ CreateAction.class
│
└─lib
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
jcommon-1.0.14.jar
junit.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
struts2-jfreechart-plugin-2.0.11.2.jar
xwork-2.0.5.jar
1.CreateAction.java
// ******************************************************************
package cn.struts2;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class CreateAction extends ActionSupport
{
private String name ;
private String password;
private String repassword;
private Date birthday;
private Date registedDay;
private int age;
private String email;
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return the repassword
*/
public String getRepassword()
{
return repassword;
}
/**
* @param repassword the repassword to set
*/
public void setRepassword(String repassword)
{
this.repassword = repassword;
}
/**
* @return the birthday
*/
public Date getBirthday()
{
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
/**
* @return the registedDay
*/
public Date getRegistedDay()
{
return registedDay;
}
/**
* @param registedDay the registedDay to set
*/
public void setRegistedDay(Date registedDay)
{
this.registedDay = registedDay;
}
/**
* @return the age
*/
public int getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age)
{
this.age = age;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
//****************************************
public String execute()throws Exception
{
return SUCCESS;
}
}
//******************************************************************
2.置全局國際化文件(兩個):
// message_en_US.properties ********************************************
create = Create Users Information
username.invalid = User Name Not Null!
password.invalid.null = Password Not Null!
password.invalid.too.short.or.long = Password should be between 6 and 10
submit = submit
//***********************************************************************
中文國際化
// message_zh_CN.properties ********************************************
create = /u521b/u5efa/u7528/u6237/u4fe1/u606f
username.invalid = /u7528/u6237/u540d/u4e0d/u80fd/u4e3a/u7a7a
password.invalid.null = /u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a
password.invalid.too.short.or.long = /u5bc6/u7801/u957f/u5ea6/u5fc5/u987b/u57286/u523010/u4e4b/u95f4
submit =/u63d0/u4ea4
//***********************************************************************
注意:如(create = /u521b/u5efa/u7528/u6237/u4fe1/u606f)等號右邊的一串亂碼是中文字符對就的ASCII碼值,如果你需要轉換,可以打開你的CMD(開始-->運行-->輸入CMD即可),輸入命令native2ascii,回車,將你的中文字符粘上,再回車就可以看到一串亂碼了.再將其COPY到相應的位置即可.
文件的名字不能亂取,XXX_en_US.properties,XXX_zh_CN.properties,XXX後面的名字是固定的,而前面的XXX是根據你的struts.xml文件中的 <constant name="struts.custom.i18n.resources" value="XXX"></constant>中的XXX而取的.本例的XXX就是message.
3.struts.xml
// ************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="create" class="cn.struts2.CreateAction" >
<result name="success">/createResult.jsp</result>
<result name="input">/create.jsp</result>
<!-- avoid refresh again -->
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token">/create.jsp</result>
</action>
</package>
</struts>
//************************************************************************
4.web.xml
// ****************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
//*************************************************************************
5.JSP文件
(1)
// createResult.jsp ************************************************************************
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> luanmad's JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is luanmad's JSP page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
User Name:${requestScope.name }<br>
Password :${requestScope.password }<br>
Age:<s:property value="age"/><br>
Birthday:<s:property value="birthday"/><br>
RegistedDay:<s:property value="registedDay"/><br>
Email:<s:property value="email"/><br>
</body>
</html>
//***********************************************************************
(2)
// create.jsp *******************************************************************
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>luanmad's JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is luanmad's JSP page ">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="create" method="post">
<!-- 防止刷新重複提交-->
<s:token></s:token>
<s:textfield name="name" label="User Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:textfield name="age" label="Age"></s:textfield>
<s:textfield name="birthday" label="Birthday"></s:textfield>
<s:textfield name="registedDay" label="RegistedDay"></s:textfield>
<s:textfield name="email" label="Email"></s:textfield>
<s:submit key="submit"></s:submit>
<s:reset label="reset"></s:reset>
</s:form>
</body>
</html>
//*******************************************************************
6.驗證文件(注意名字要與你的Action名字一樣,後面再跟固定的(-validation.xml)如(CreateAction-validation.xml)
CreateAction-validation.xml:
// ****************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<!-- 格式的寫法可以參照XWork Validator 1.0.2.dtd -->
<!-- 參數設置參照xwork-2.0.5.jar 下的com.opensymphony.xwork2.validator.validators/default.xml -->
<validators>
<!--驗證誰, 用誰來驗證 -->
<field name="name">
<field-validator type="requiredstring">
<!--requiredstring對應的類的方法裏的參數名trim 如public void setTrim(boolean trim)裏的trim -->
<param name="trim">true</param>
<!-- message key的key內容是I18N裏(即baseName_zh_CN.properties和baseName_en_US.properties中)定義的字段名即等號在邊的名字如(username.invalid = 名字不能爲空)-->
<message key="username.invalid"></message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="password.invalid.null"></message>
</field-validator>
</field>
<field name="password">
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">16</param>
<message key="password.invalid.too.short.or.long"></message>
</field-validator>
</field>
<!-- 以下未做國際化 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message>birthday not null!</message>
</field-validator>
<field-validator type="date">
<param name="min">2000-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message>email format error!</message>
</field-validator>
</field>
</validators>
//*******************************************************************************************
注意導入相應的包.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.