知识大全 Struts2 国际化与防止刷新重复提交表单
Posted 文件
篇首语:守株待兔只能得一餐饱,主动出击方能丰衣足食。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Struts2 国际化与防止刷新重复提交表单相关的知识,希望对你有一定的参考价值。
Struts2 国际化与防止刷新重复提交表单 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
本实例用两个页面(create jsp createResult jsp) 一个Action(CreateAction) 一个验证文件(CreateAction validation xml) 两个Struts 国际化文件(message_en_US properties message_zh_CN properties) 还有一个struts xml(必有的) 创建用户成功之后 显示刚才创建的信息 不成功则显示错误提示 错误提示使用了Struts 国际化来显示 输入合法性就用了用struts 的验证机制验证
如下结构图 好好对照:
K:\\ECLIPSWORKS\\STRUTS TEST│ classpath│ mymetadata│ project│├─ myeclipse├─src│ │ message_en_US properties│ │ message_zh_CN properties│ │ struts xml│ ││ └─cn│ └─struts │ 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│ └─struts │ CreateAction validation xml│ CreateAction class│└─libmons logging jarfreemarker jarjmon jarjunit jarognl jarstruts core jarstruts jfreechart plugin jarxwork jar
CreateAction java // ******************************************************************package cn struts ;
import java util Date;
import opensymphony xwork ActionSupport;
public class CreateAction extends ActionSupportprivate 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 Exceptionreturn SUCCESS;
置全局Struts 国际化文件(两个):
// message_en_US properties ********************************************create = Create Users Informationusername invalid = User Name Not Null!password invalid null = Password Not Null!password invalid too short or long = Password should be beeen and submit = submit//***********************************************************************中文国际化// message_zh_CN properties ********************************************create = \\u b\\u efa\\u \\u \\u fe \\u fusername invalid = \\u \\u \\u d\\u e d\\u fd\\u e a\\u a apassword invalid null = \\u bc \\u \\u e d\\u fd\\u e a\\u a apassword invalid too short or long = \\u bc \\u \\u f\\u ea \\u fc \\u b\\u \\u \\u e b\\u f submit =\\u d \\u ea
//***********************************************************************
注意:如(create = \\u b\\u efa\\u \\u \\u fe \\u f)等号右边的一串乱码是中文字符对就的ASCII码值 如果你需要转换 可以打开你的CMD(开始 >运行 >输入CMD即可) 输入命令native ascii 回车 将你的中文字符粘上 再回车就可以看到一串乱码了 再将其COPY到相应的位置即可
文件的名字不能乱取 XXX_en_US properties XXX_zh_CN properties XXX后面的名字是固定的 而前面的XXX是根据你的struts xml文件中的
< CONSTANT name= struts custom i n resources value= XXX >中的XXX而取的 本例的XXX就是message
struts xml // ************************************************************************< ?xml version= encoding= UTF ?> dtd >
< STRUTS>< CONSTANT name= struts custom i n resources value= message >< /CONSTANT>< PACKAGE name= struts extends= struts default >
< ACTION class=cn struts CreateAction name= create >
< /PACKAGE>
//************************************************************************
web xml // ****************************************************************
//*************************************************************************
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 -equiv=pragma>< META content=no-cache -equiv=cache-control>< META content=0 -equiv=expires> < META content=keyword1,keyword2,keyword3 -equiv=keywords>< META content="This is luanmad\'s JSP page" -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 -equiv=pragma>< META content=no-cache -equiv=cache-control>< META content=0 -equiv=expires>< META content=keyword1,keyword2,keyword3 -equiv=keywords>< META content="This is luanmad\'s JSP page " -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 下的.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 = 名字不能为空)-->
相关参考