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>
Step2:
创建成功页:success.jsp
<%@ 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:
创建Action-Class:
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; } } Step4:
配置Struts.xml(默认创建在src目录下):
<?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>
Step5:
配置web.xml,加入Filter:
<?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>
Step6:
加入Validators.默认和你的Action建一个包下,其命名规则为<ActionClassName>-validation.xml
或是<ActionClassName>-<ActionAliasName>-validation.xml.
在本例中名称为:QuizAction-validation.xml
<?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_source->Struts2基本表单验证.rar
posted on 2007-11-24 07:42 々上善若水々 阅读(8173) 评论(4) 编辑 收藏 所属分类: Struts2
Powered by: BlogJava Copyright © 々上善若水々