struts2 国际化,防止刷新重复提交表单与邮箱格式验证

来源:互联网 发布:大学生网络贷款的认识 编辑:程序博客网 时间:2024/05/16 06:50

 

            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>


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luanmad/archive/2008/10/29/3173866.aspx

原创粉丝点击