Step 1:
创建输入页:index.jsp
<
html
>
<
head
>
<
title
>
Validation - Basic
</
title
>
<
s:head
/>
</
head
>

<
body
>

<
b
>
What is your favorite color?
</
b
>
<
p
/>

<
s:form
method
="post"
>
<
s:textfield
label
="Name"
name
="name"
/>
<
s:textfield
label
="Age"
name
="age"
/>
<
s:textfield
label
="password"
name
="password"
/>
<
s:submit
/>
</
s:form
>
</
body
>
</
html
>
<%
@ page language
=
"
java
"
pageEncoding
=
"
GB18030
"
%>
<%
@taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
<
html
>
<
head
>
<
title
>
validation test
</
title
>
</
head
>
<
body
>
Your name is :
<
s:property value
=
"
name
"
/>
<
br
/>
Your age is :
<
s:property value
=
"
age
"
/>
<
br
/>
Your age is :
<
s:property value
=
"
password
"
/>
</
body
>
</
html
>
Step3:
package
com;
import
com.opensymphony.xwork2.ActionSupport;
/** */
/** * Action类 * @author zdw * */
public
class
QuizAction
extends
ActionSupport
{ private static final long serialVersionUID = 3124124L;
private String name; private int age; private String password;
public String getPassword()
{ return password; }
public void setPassword(String password)
{ this.password = password; }
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
public int getAge()
{ return age; }
public void setAge(int age)
{ this.age = age; }
@Override public String execute() throws Exception
{ return SUCCESS; }
}
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"
>< struts >
< package name ="com" extends ="struts-default" >
< action name ="validation" class ="com.QuizAction" >
<!-- input & result不能少 -->
< result name ="input" > /index.jsp </ result >
< result name ="success" > /success.jsp </ result >
</ action >
</ package >
</ struts >
<?
xml version="1.0" encoding="UTF-8"
?>< web-app version ="2.4" 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 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
>
<!--
此xml的DTD声明可以到xwork包下的xwork-validator-1.0.2.dtd里面复制
-->
<
validators
>
<!--
对应Action类里面的属性
-->
<
field
name
="name"
>
<
field-validator
type
="requiredstring"
>
<
message
>
name is required
</
message
>
</
field-validator
>
</
field
>
<
field
name
="age"
>
<
field-validator
type
="int"
>
<
param
name
="min"
>
2
</
param
>
<
param
name
="max"
>
10
</
param
>
<
message
>
between 2 and 10
</
message
>
</
field-validator
>
</
field
>
<!--
Plain validator Syntax
-->
<!--
<field name="password">
<field-validator type="stringlength">
<param name="minLength">4</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>require between 4 and 10</message>
</field-validator> </field>
-->
<!--
Field validator Syntax
-->
<
validator
type
="stringlength"
>
<
param
name
="fieldName"
>
password
</
param
>
<
param
name
="minLength"
>
4
</
param
>
<
param
name
="maxLength"
>
10
</
param
>
<
param
name
="trim"
>
true
</
param
>
<
message
>
require between 4 and 10
</
message
>
</
validator
>
</
validators
>
本文介绍了一个使用Struts2框架实现的简单验证流程案例。包括了从创建表单页面到配置验证规则的过程。该案例涵盖了如何设置输入字段、定义Action类及其实现,以及如何配置Struts2拦截器进行验证。

1311

被折叠的 条评论
为什么被折叠?



