现在的位置: 首页 > 综合 > 正文

Struts之Validator学习

2013年09月14日 ⁄ 综合 ⁄ 共 3807字 ⁄ 字号 评论关闭
来源与 Struts编程基础与实例详解
struts-config.xml如下(这里没有任何意义,因为我的目的只是验证输入,所以随便做个样子就可以了):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
    
<data-sources />
    
<form-beans >
    
<form-bean name="myValidatorForm" type="com.form.MyValidatorForm" />
    

    
</form-beans>

    
<global-exceptions />
    
<global-forwards />
    
<action-mappings >
    
<action
      
attribute="myValidatorForm"
      input
="/index.jsp"
      name
="myValidatorForm"
      path
="/myValidator"
      scope
="request"
      type
="com.action.MyValidatorAction" />
    

    
</action-mappings>

    
<message-resources parameter="com.ApplicationResources" />

    
<!-- 校验框架的定义,必须要添加!!-->
    
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        
<set-property property="pathnames"
            value
="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
    
</plug-in>
</struts-config>

 index.jsp页面:

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
    
<head>
        
<title>JSP for MyValidatorForm form</title>
    
</head>
    
<body>
    
<br/>
        
<html:form action="/myValidator">
            password : 
<html:text property="password"/><html:errors property="password"/><br/>
            phone : 
<html:text property="phone"/><html:errors property="phone"/><br/>
            zipCode : 
<html:text property="zipCode"/><html:errors property="zipCode"/><br/>
            address : 
<html:text property="address"/><html:errors property="address"/><br/>
            firstName : 
<html:text property="firstName"/><html:errors property="firstName"/><br/>
            lastName : 
<html:text property="lastName"/><html:errors property="lastName"/><br/>
            
<html:submit/><html:cancel/>
        
</html:form>
    
</body>
</html>

外观如:

与其相对的Bean,MyValidatorForm.java代码如下:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package com.form;

import org.apache.struts.validator.ValidatorForm;

/** 
 * MyEclipse Struts
 * Creation date: 05-11-2007
 * 
 * XDoclet definition:
 * @struts.form name="myValidatorForm"
 
*/

public class MyValidatorForm extends ValidatorForm {         //这是唯一的特别之处
    
/*
     * Generated fields
     
*/


    
/** password property */
    
private String password;

    
/** phone property */
    
private String phone;

    
/** zipCode property */
    
private String zipCode;

    
/** address property */
    
private String address;

    
/** firstName property */
    
private String firstName;

    
/** lastName property */
    
private String lastName;

    
/*
     * Generated Methods
     
*/


    
/** 
     * Returns the password.
     * 
@return String
     
*/

    
public String getPassword() {
        
return password;
    }


    
/** 
     * Set the password.
     * 
@param password The password to set
     
*/

    
public void setPassword(String password) {
        
this.password = password;
    }


    
/** 
     * Returns the phone.
     * 
@return String
     
*/

    
public String getPhone() {
        
return phone;
    }


    
/** 
     * Set the phone.
     * 
@param phone The phone to set
     
*/

    
public void setPhone(String phone) {
        
this.phone = phone;
    }


    
/** 
     * Returns the zipCode.
     * 
@return String
     
*/

    
public String getZipCode() {
        
return zipCode;
    }


    
/** 
     * Set the zipCode.
     * 
@param zipCode The zipCode to set
     
*/

    
public void setZipCode(String zipCode) {
        
this.zipCode = zipCode;
    }


    
/** 
     * Returns the address.
     * 
@return String
     
*/

    
public String getAddress() 

抱歉!评论已关闭.