Struts2 国际化与防止刷新重复提交表单

开发 后端
本实例主要是功能是实现Struts2 国际化,防止刷新得利提交表单,利用struts2的验证机制验证字符输入的合法性,邮箱输入的正确性。

本实例用两个页面(create.jsp,createResult.jsp),一个Action(CreateAction),一个验证文件(CreateAction-validation.xml),两个Struts2 国际化文件(message_en_US.properties,message_zh_CN.properties),还有一个struts.xml(必有的).创建用户成功之后,显示刚才创建的信息,不成功则显示错误提示,错误提示使用了Struts2 国际化来显示,输入合法性就用了用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.置全局Struts2 国际化文件(两个):

// 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">中的XXX而取的.本例的XXX就是message.

3.struts.xml
//  ************************************************************************
< ?xml version="1.0" encoding="UTF-8"?>
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 class=cn.struts2.CreateAction name="create">
        /createResult.jsp< /RESULT>
        < RESULT name="input">/create.jsp< /RESULT>
       
       
       
        < INTERCEPTOR-REF name="token">< /INTERCEPTOR-REF>
        < INTERCEPTOR-REF name="defaultStack">< /INTERCEPTOR-REF>
        /create.jsp            
       
      

  < /PACKAGE>

//************************************************************************

4.web.xml 
// ****************************************************************

    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
      < 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" %>


 
    < BASE href="<%=basePath%>">
   
   
   
    < META content=no-cache http-equiv=pragma>
    < META content=no-cache http-equiv=cache-control>
    < META content=0 http-equiv=expires>   
    < META content=keyword1,keyword2,keyword3 http-equiv=keywords>
    < META content="This is luanmad's JSP page" http-equiv=description>
    < !--
    < LINK rel=stylesheet type=text/css href="styles.css">
    -->


 
 
    User Name:${requestScope.name }

    Password :${requestScope.password }

    Age:< ?xml:namespace prefix = s />< s:property value="age">

    Birthday:< s:property value="birthday">< /s:property>

    RegistedDay:< s:property value="registedDay">< /s:property>

    Email:< s:property value="email">< /s:property>

   
 

//***********************************************************************

(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"%>


   
       

       

        < META content=no-cache http-equiv=pragma>
        < META content=no-cache http-equiv=cache-control>
        < META content=0 http-equiv=expires>
        < META content=keyword1,keyword2,keyword3 http-equiv=keywords>
        < META content="This is luanmad's JSP page " http-equiv=description>
        < !--
   
    -->

   

   
               
        < s:form method="post" action="create">
            < !-- 防止刷新重复提交-->
            < 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 name="email" label="Email">
            < s:submit key="submit">< /s:submit>
            < s:reset label="reset">< /s:reset>

       
   

//*******************************************************************

6.验证文件(注意名字要与你的Action名字一样,后面再跟固定的(-validation.xml)如(CreateAction-validation.xml)

CreateAction-validation.xml:
// ****************************************************************************
< ?xml version="1.0" encoding="UTF-8"?>
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>
        < /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">
            1< /PARAM>
            < PARAM name="max">150< /PARAM>
            < MESSAGE>age should be between ${min} and ${max}

        < /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}
        < /FIELD-VALIDATOR>
   
   
    < FIELD name="email">
        < FIELD-VALIDATOR type="email">
            < MESSAGE>email format error!
        < /FIELD-VALIDATOR>
    < /FIELD>
   

【编辑推荐】

  1. 在Eclipse中开发struts应用程序
  2. 手把手教你在Eclipse中配置开发Struts
  3. Eclipse下开发struts完整解决乱码问题
  4. Struts相关背景介绍
  5. 使用Easy Struts for Eclipse开发Struts
责任编辑:张燕妮 来源: csdn.net
相关推荐

2009-06-25 16:04:30

2011-08-19 13:13:14

struts2Java

2009-02-04 15:04:13

2009-06-05 09:40:59

2010-11-23 16:56:04

mysql表单

2013-11-13 11:01:14

表单表单重复提交表单策略

2013-11-13 14:39:53

表单提交开发

2009-07-29 09:54:34

struts2和str

2009-06-18 11:37:24

Struts2中ForJavaScript

2009-06-04 10:44:18

struts2 乱码解决办法

2011-05-13 09:53:02

strutsAjax

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2022-11-11 07:34:43

2009-06-08 16:44:00

struts2 ogn

2009-07-03 09:35:57

Struts2 JSP

2011-07-08 11:13:42

Cocoa Touch XCode

2009-06-04 08:34:24

Struts2配置struts.xml

2022-11-15 07:39:48

2022-11-17 07:43:13

点赞
收藏

51CTO技术栈公众号