熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts2 國際化與防止刷新重復提交表單

2013-11-23 20:13:53  來源: Java開源技術 

  本實例用兩個頁面(createjspcreateResultjsp)一個Action(CreateAction)一個驗證文件(CreateActionvalidationxml)兩個Struts 國際化文件(message_en_USpropertiesmessage_zh_CNproperties)還有一個strutsxml(必有的)創建用戶成功之後顯示剛才創建的信息不成功則顯示錯誤提示錯誤提示使用了Struts 國際化來顯示輸入合法性就用了用struts的驗證機制驗證

  如下結構圖好好對照:

  

  

  K:\ECLIPSWORKS\STRUTSTEST
│  classpath
│  mymetadata
│  project

├─myeclipse
├─src
│  │  message_en_USproperties
│  │  message_zh_CNproperties
│  │  strutsxml
│  │
│  └─cn
│      └─struts
│              CreateActionvalidationxml
│              CreateActionjava

└─WebRoot
│  createjsp
│  createResultjsp

├─METAINF
│      MANIFESTMF

└─WEBINF
│  webxml

├─classes
│  │  message_en_USproperties
│  │  message_zh_CNproperties
│  │  strutsxml
│  │
│  └─cn
│      └─struts
│              CreateActionvalidationxml
│              CreateActionclass

└─lib

commonsloggingjar
freemarkerjar
jcommonjar
junitjar
ognljar
strutscorejar
strutsjfreechartpluginjar
xworkjar

  CreateActionjava
// ******************************************************************
package cnstruts;

  import javautilDate;

  import comopensymphonyxworkActionSupport;

  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)
{
thisname = name;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
thispassword = password;
}
/**
* @return the repassword
*/
public String getRepassword()
{
return repassword;
}
/**
* @param repassword the repassword to set
*/
public void setRepassword(String repassword)
{
thisrepassword = repassword;
}
/**
* @return the birthday
*/
public Date getBirthday()
{
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday)
{
thisbirthday = birthday;
}
/**
* @return the registedDay
*/
public Date getRegistedDay()
{
return registedDay;
}
/**
* @param registedDay the registedDay to set
*/
public void setRegistedDay(Date registedDay)
{
thisregistedDay = registedDay;
}
/**
* @return the age
*/
public int getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age)
{
thisage = age;
}

public String getEmail()
{
return email;
}
public void setEmail(String email)
{
thisemail = email;
}
//****************************************

public String execute()throws Exception
{
return SUCCESS;
}

}

  置全局Struts 國際化文件(兩個):



  

  // message_en_USproperties ********************************************
create = Create Users Information
usernameinvalid = User Name Not Null!
passwordinvalidnull = Password Not Null!
passwordinvalidtooshortorlong = Password should be between and
submit = submit
//***********************************************************************
中文國際化
// message_zh_CNproperties ********************************************
create = \ub\uefa\u\u\ufe\uf
usernameinvalid = \u\u\ud\ued\ufd\uea\uaa
passwordinvalidnull = \ubc\u\ued\ufd\uea\uaa
passwordinvalidtooshortorlong = \ubc\u\uf\uea\ufc\ub\u\u\ueb\uf
submit =\ud\uea

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

  注意:如(create = \ub\uefa\u\u\ufe\uf)等號右邊的一串亂碼是中文字符對就的ASCII碼值如果你需要轉換可以打開你的CMD(開始>運行>輸入CMD即可)輸入命令nativeascii回車將你的中文字符粘上再回車就可以看到一串亂碼了再將其COPY到相應的位置即可

  文件的名字不能亂取XXX_en_USpropertiesXXX_zh_CNpropertiesXXX後面的名字是固定的而前面的XXX是根據你的strutsxml文件中的



  

  < CONSTANT name=strutscustominresources value=XXX>中的XXX而取的本例的XXX就是message

  strutsxml
//  ************************************************************************
< ?xml version= encoding=UTF?>
dtd>

  < STRUTS>
< CONSTANT name=strutscustominresources value=message>< /CONSTANT>

< PACKAGE name=struts extends=strutsdefault>

  < ACTION class=cnstrutsCreateAction name=create>
/createResultjsp< /RESULT>
< RESULT name=input>/createjsp< /RESULT>



< INTERCEPTORREF name=token>< /INTERCEPTORREF>
< INTERCEPTORREF name=defaultStack>< /INTERCEPTORREF>
/createjsp            

  < /PACKAGE>

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

  webxml 
// ****************************************************************

xmlns=""
xmlns:xsi="-instance"
xsi:schemaLocation="
-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 >



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

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


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28156.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.