struts 的Validator应用需要配置的关键地方.
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
input="post.jsp"
name="userForm"
path="/user"
scope="request"
type="com.lunzi.struts.action.UserAction" />
</action-mappings>
<message-resources parameter="com.lunzi.struts.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>
<?xml version="1.0" encoding="iso-8859-1"?>
<!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">
validation.xml
<form-validation>
<formset>
<form name="userForm">
<field
property="name"
depends="required,,minlength,maxlength">
<arg0 key="label.username"/>
<arg1 key="${var:maxlength}" resource="false" />
<arg2 key="${var:minlength}" resource="false" />
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
post.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"%>
<html>
<head>
<title>JSP for userForm form</title>
</head>
<body>
<html:form action="/user">
name : <html:text property="name"/><html:errors property="name"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</htm
struts 的Validator
最新推荐文章于 2024-09-03 23:03:56 发布