日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Struts2 國際化與防止刷新重復提交表單

本實例用兩個頁面(create.jsp,createResult.jsp),一個Action(CreateAction),一個驗證文件(CreateAction-validation.xml),兩個Struts2 國際化文件(message_en_US.properties,message_zh_CN.properties),還有一個struts.xml(必有的).創(chuàng)建用戶成功之后,顯示剛才創(chuàng)建的信息,不成功則顯示錯誤提示,錯誤提示使用了Struts2 國際化來顯示,輸入合法性就用了用struts2的驗證機制驗證.

十年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站定制,個人網(wǎng)站制作服務,為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設計流程、步驟,成功服務上千家企業(yè)。為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制高端網(wǎng)站建設服務,專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對成都PE包裝袋等多個方面,擁有豐富建站經(jīng)驗。

如下結構圖,好好對照:

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碼值,如果你需要轉(zhuǎn)換,可以打開你的CMD(開始-->運行-->輸入CMD即可),輸入命令native2ascii,回車,將你的中文字符粘上,再回車就可以看到一串亂碼了.再將其COPY到相應的位置即可.

文件的名字不能亂取,XXX_en_US.properties,XXX_zh_CN.properties,XXX后面的名字是固定的,而前面的XXX是根據(jù)你的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 -->
< !-- 參數(shù)設置參照xwork-2.0.5.jar 下的com.opensymphony.xwork2.validator.validators/default.xml -->
< VALIDATORS>
   
   
    < !--驗證誰, 用誰來驗證 -->
    < FIELD name="name">
        < FIELD-VALIDATOR type="requiredstring">
            < !--requiredstring對應的類的方法里的參數(shù)名trim 如public void setTrim(boolean trim)里的trim -->
            < PARAM name="trim">true< /PARAM>
            < !-- message key的key內(nèi)容是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中開發(fā)struts應用程序
  2. 手把手教你在Eclipse中配置開發(fā)Struts
  3. Eclipse下開發(fā)struts完整解決亂碼問題
  4. Struts相關背景介紹
  5. 使用Easy Struts for Eclipse開發(fā)Struts

文章標題:Struts2 國際化與防止刷新重復提交表單
標題URL:http://m.5511xx.com/article/dpdidod.html