查看上一篇博文的程序

Struts2表单基本验证_java


QuizAction.java

package com;

import com.opensymphony.xwork2.ActionSupport;

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;
	}
}



QuizAction-validatio.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/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>

struts.xml

<?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>



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>vvvvv</display-name>
  
<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>



index.jsp

<%@ page language="java" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Validation - Basic</title>
    <s:head/>
</head>

<body>

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

<s:form action="validation" 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>



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>



Struts2表单基本验证_xml_02