您的位置:首页 > 编程语言 > Java开发

Struts 验证框架实例

2008-12-12 08:47 471 查看
]Struts 验证框架实例
login.jsp:
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<html>
<head>
<title><bean:message key="msg.title" /></title>

</head>
<body>
<html:javascript formName="loginForm"/>
<logic:messagesPresent>
<html:messages id="error">
<li><b><font color=red><bean:write name="error"/></font></b></li>
</html:messages>
<hr/>
</logic:messagesPresent>

<html:form action="/login"  method="post" onsubmit="return

validateLoginForm(this);">
username : <html:text property="username"/><br/>
userpassword : <html:password property="userpassword"/><br/>
repeate password : <html:password property="userpassword2"/>
<br/>
userage : <html:text property="userage"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html><!--
<html:form action="/login"  method="post">
username : <html:text property="username"/><html:errors

property="username"/><br/>
userpassword : <html:password property="userpassword"/><html:errors

property="userpassword"/><br/>
repeate password : <html:password property="userpassword2"/>
<br/>
userage : <html:text property="userage"/><html:errors

property="userage"/><br/>
<html:submit/><html:cancel/>
</html:form>
-->

ApplicationResources.properties:

# Resources for parameter 'com.web.ApplicationResources'
# Project P/Valid
# Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
login.id=User ID
login.pwd=User Password
login.age=User Age
msg.login=Please Input User Info
msg.title=Demo
user.pwd=Password
user.rpwd=Repeat word
test.valid={0} is not equal as {1}

error.required=<font color=red>{0}test</font>
error.mask=<font color=red>{0}test2</font>
reg.uemail=email

validation.xml:
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration

1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>

<formset>
<!--
<global>
<constant>
<constant-name>phone</constant-name>
<constant-value>^/(?(/d{3})/)?[-| ]?(/d{3})[-| ]?(/d{4})$</constant-value>
</constant>
<constant>
<constant-name>zip</constant-name>
<constant-value>^/d{5}/d*$</constant-value>
</constant>
<constant>
<constant-name>currency</constant-name>
<constant-value>^/d{1,3}(,?/d{1,3})*/.?(/d{1,2})?$</constant-value>
</constant>
</global>

-->

<form name="loginForm">

<field property="username" depends="required" >
<arg key="login.id" resource="true" position="0" />
</field>
<field property="userage" depends="mask" >
<!--<arg key="login.age" resource="true" position="0" />-

->
<arg0  key="login.age" name="mask"  resource="true"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
<field property="userpassword"

depends="required,minlength,maxlength" >
<arg key="login.pwd" resource="true" position="0" />
<arg key="${var:minlength}" resource="false" position="1"

name="minlength" />
<arg key="${var:maxlength}" resource="false" position="1"

name="maxlength" />
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>8</var-value>
</var>
</field>

<field property="userpassword2" depends="required,validwhen">
<arg key="user.rpwd" position="0"/>
<arg key="user.pwd" position="1"/>
<msg name="validwhen" key="test.valid" />
<var>
<var-name>test</var-name>
<var-value>((*this*==null)or

(*this*==userpassword))</var-value>
</var>
</field>

<!-- 格式:字母或"_"开头 + 3~10个字母、数字或"_" + "@" + 3~10个字

母、数字或"_" + "." + 2~4个字母
<field property="uemail" depends="required,mask">
<msg key="error.required" name="required" />
<msg key="error.mask" name="mask"/>
<arg0 key="reg.uemail" />
<var>
<var-name>mask</var-name>
<var-value>
^(([/_a-zA-Z])/w{3,10})+@/w{3,10}/.[a-zA-Z]{2,4}$
</var-value>
</var>
</field>
-->
</form>
</formset>
</form-validation>

LoginForm:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.web.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class LoginForm extends ValidatorForm {
private String userpassword;
private String userpassword2;
private String username;
private String userage;
//public ActionErrors validate(ActionMapping mapping,
//          HttpServletRequest request) {
//      return null;
//  }
//
//public void reset(ActionMapping mapping, HttpServletRequest request) {
//  }

public String getUserpassword() {
return userpassword;
}

public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
public String getUserage() {
return userage;
}

public void setUserage(String userage) {
this.userage = userage;
}

public String getUserpassword2() {
return userpassword2;
}

public void setUserpassword2(String userpassword2) {
this.userpassword2 = userpassword2;
}
}

Action就是一个空的转向Action

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="loginForm" type="com.web.form.LoginForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
validate="true"
type="com.web.action.LoginAction">
<forward
name="su"
path="/su.jsp"
redirect="true" />
</action>

</action-mappings>
<message-resources parameter="com.web.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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息