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

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

十年专注成都网站制作,成都企业网站定制,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于成都企业网站定制,高端网页制作,对成都PE包装袋等多个方面,拥有丰富建站经验。

如下结构图,好好对照:

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

分享名称:Struts2 国际化与防止刷新重复提交表单
当前链接:http://www.shufengxianlan.com/qtweb/news16/235066.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联