1. maven2 configuration
<dependency>
<groupid>org.springmodules</groupid>
<artifactid>spring-modules-validation</artifactid>
<version>0.8a</version>
</dependency>
2.spring applicationContext.xml
<bean id="validatorFactory"
class="org.springmodules.validation.commons.DefaultValidatorFactory">
<property name="validationConfigLocations">
<list>
<value>/WEB-INF/validator-rules.xml</value>
<value>/WEB-INF/validation.xml</value>
</list>
</property>
</bean>
<bean id="beanValidator"
class="org.springmodules.validation.commons.DefaultBeanValidator">
<property name="validatorFactory" ref="validatorFactory" />
</bean>
3.Download validator-rules.xml from SpringModules sample
Download spring modules and you can use validator-rules.xml in spring-modules-validation-commons-samples-src.zip as sample
4.Use spring modules validation tag library.
web.xml
<jsp-config>
<taglib>
<taglib-uri>http://www.springmodules.org/tags/commons-validator</taglib-uri>
<taglib-location>/WEB-INF/tld/spring-commons-validator.tld</taglib-location>
</taglib>
</jsp-config>
jsp
<%@ taglib uri="http://www.springmodules.org/tags/commons-validator" prefix="validator" %>
5.Create validation.js
web.xml
<servlet>
<servlet-name>javascript</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javascript</servlet-name>
<url-pattern>/validation.js</url-pattern>
</servlet-mapping>
create javascript-servlet.xml
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/validation.js">
staticJavascriptController
</prop>
</props>
</property>
</bean>
<bean id="staticJavascriptController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName">
<value>/common/validator.jsp</value>
</property>
</bean>
create validation.jsp
<%@ page language="java" contentType="javascript/x-javascript" %>
<%@ taglib uri="http://www.springmodules.org/tags/commons-validator" prefix="validator" %>
<validator:javascript dynamicJavascript="false" staticJavascript="true"/>
6.Setting validation.xml and modify jsp form tag
jsp form sample
<form:form commandName="member" onsubmit="return validateLoginForm(this);">
<form:errors path="*" />
<table>
<tr>
<td>
Account
</td>
<td>
<form:input path="account"/>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<form:password path="password"/>
</td>
</tr>
<tr>
<td>
<input type="submit">
</td>
</tr>
</table>
</form:form>
<validator:javascript formName="loginForm" staticJavascript="false"/>
validation.xml
<form-validation>
<formset>
<form
name="loginForm">
<field
property="account"
depends="required">
<arg
key="prompt.account"/>
</field>
<field
property="password"
depends="required">
<arg
key="prompt.password"/>
</field>
</form>
</formset>
</form-validation>
沒有留言:
張貼留言