java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2020年08月23日
 
Struts2 国际化与防止刷新重复提交表单

本实例用两个页面(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>

上一篇:Hibernate拦截器简单描述

下一篇:集成 Struts、Tiles 和 JavaServer Faces