本實例用兩個頁面(create
如下結構圖
K:\ECLIPSWORKS\STRUTS
│
│
│
│
├─
├─src
│ │ message_en_US
│ │ message_zh_CN
│ │ struts
│ │
│ └─cn
│ └─struts
│ CreateAction
│ CreateAction
│
└─WebRoot
│ create
│ createResult
│
├─META
│ MANIFEST
│
└─WEB
│ web
│
├─classes
│ │ message_en_US
│ │ message_zh_CN
│ │ struts
│ │
│ └─cn
│ └─struts
│ CreateAction
│ CreateAction
│
└─lib
commons
freemarker
jcommon
junit
ognl
struts
struts
xwork
// ******************************************************************
package cn
import java
import com
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
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this
}
/**
* @return the repassword
*/
public String getRepassword()
{
return repassword;
}
/**
* @param repassword the repassword to set
*/
public void setRepassword(String repassword)
{
this
}
/**
* @return the birthday
*/
public Date getBirthday()
{
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday)
{
this
}
/**
* @return the registedDay
*/
public Date getRegistedDay()
{
return registedDay;
}
/**
* @param registedDay the registedDay to set
*/
public void setRegistedDay(Date registedDay)
{
this
}
/**
* @return the age
*/
public int getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age)
{
this
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this
}
//****************************************
public String execute()throws Exception
{
return SUCCESS;
}
}
// message_en_US
create = Create Users Information
username
password
password
submit = submit
//***********************************************************************
中文國際化
// message_zh_CN
create = \u
username
password
password
submit =\u
//***********************************************************************
注意:如(create = \u
文件的名字不能亂取
< CONSTANT name=
// ************************************************************************
< ?xml version=
< STRUTS>
< CONSTANT name=
< PACKAGE name=
< ACTION class=cn
< RESULT name=
< INTERCEPTOR
< INTERCEPTOR
< /PACKAGE>
//************************************************************************
// ****************************************************************
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 --> 1< /PARAM>
< !-- 參數設置參照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 = 名字不能為空)-->
< /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">
< 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