Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
valang client validation with annotated controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • valang client validation with annotated controller

    Hi,

    I am using spring-modules 0.9. I am following chapter 17 on how to set up client validation with valang.

    The server side validation is working.

    But when I put <valang:validate commandName="userMessage" /> within <form:form commandName="userMessage"> ... </form:form>, it said: "org.apache.jasper.JasperException: java.lang.IllegalArgumentException: No valang rules for command 'userMessage' were found in the page context."

    Does anyone know about this problem? What can I do to make valang know that userMessage has rules?

    Thanks,

    Derek

  • #2
    I'm getting the same problem. Did you resolve this yet?

    Comment


    • #3
      I've sorted it out. No thanks to any of the documentation.

      I've been testing it in the petClinic application, here's what I've done in order to get client side and server side validation working.

      petclinic-servlet.xml:
      add
      Code:
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" />
      and
      Code:
      <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        	<property name="order"><value>3</value></property>
          <property name="interceptors">
              <list>
                  <bean class="org.springmodules.validation.valang.javascript.taglib.ValangRulesExportInterceptor" />
              </list>
          </property>
      	</bean>
      create an xml file for defining the validation stuff, and put this in it:
      Code:
      	<?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="ht tp :/ /ww w.springframework.org/schema/beans"
             xmlns:xsi="ht tp : / /ww w.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="ht tp://ww w.springframework.org/schema/beans
                                  ht tp :/ /ww w.springframework.org/schema/beans/spring-beans-2.0.xsd">
      	
      	
      	
      	<bean id="ownerValidator" class="org.springmodules.validation.valang.ValangValidator">
      	  <property name="valang">
      	    <value>
      	    <![CDATA[
      	      { firstName : length(?) < 30 : 'First name too long' }
      	      { lastName : length(?) < 50 : 'Last name too long'  }
      	    ]]>
      	    </value>
      	  </property>
      </bean>
      </beans>
      make sure spring picks this file up by referencing it in your web.xml:
      Code:
      <param-name>contextConfigLocation</param-name>
      		<param-value>
      		/WEB-INF/applicationContext-jdbc.xml
      		/WEB-INF/validation.xml
      		</param-value>
      In the jsp page (if you're trying this out on petclinic - I've used ownerForm.jsp) make sure you reference the varlang tag library

      Code:
      <%(at) taglib uri="ht tp :/ / www .spring modules.org/tags/valang" prefix="valang" >
      and plonk the codebase in the head of the page somewhere
      Code:
      <valang:codebase includeScriptTags="true" fieldErrorsIdSuffix="_error" globalErrorsId="global_error"/>
      All the examples I've seen tell you that you reference the commandName of the form in the valang:validate tag. I've done this :
      Code:
      <form:form modelAttribute="owner"  >
      <valang:validate commandName="ownerValidator" />
      and it works just fine.

      In the controller (AddOwnerForm.java) we've got this
      Code:
      package org.springframework.samples.petclinic.web;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.samples.petclinic.Clinic;
      import org.springframework.samples.petclinic.Owner;
      import org.springframework.samples.petclinic.validation.OwnerValidator;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.validation.BindingResult;
      import org.springframework.validation.Validator;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.SessionAttributes;
      import org.springframework.web.bind.support.SessionStatus;
      import org.springframework.web.servlet.mvc.SimpleFormController;
      import org.springmodules.validation.valang.ValangValidator;
      
      /**
       * JavaBean form controller that is used to add a new <code>Owner</code> to
       * the system.
       *
       * author Juergen Hoeller
       * author Ken Krebs
       */
      (at)Controller
      (at)RequestMapping("/addOwner.do")
      (at)SessionAttributes(types = Owner.class)
      public class AddOwnerForm extends SimpleFormController{
      
      	private  Clinic clinic;
      	private ValangValidator ownerValidator;
      	
      	(at)Autowired
      	public AddOwnerForm(Clinic clinic, ValangValidator ownerValidator) {
      		this.clinic = clinic;
      		this.ownerValidator = ownerValidator;
      	}
      
      	(at)RequestMapping(method = RequestMethod.GET)
      	public String setupForm(Model model) {
      		Owner owner = new Owner();
      		model.addAttribute(owner);
      		model.addAttribute("ValangRules.ownerValidator", ownerValidator.getRules());
      		
      		return "ownerForm";
      	}
      
      	(at)RequestMapping(method = RequestMethod.POST)
      	public String processSubmit((at)ModelAttribute Owner owner, BindingResult result, SessionStatus status) {
      		ownerValidator.validate(owner, result);
      		if (result.hasErrors()) {
      			return "ownerForm";
      		}
      		else {
      			this.clinic.storeOwner(owner);
      			status.setComplete();
      			return "redirect:owner.do?ownerId=" + owner.getId();
      		}
      	}
      
      	
      }
      And thats it. This should take care of the client side and the server side validation.

      Hope this helps someone.

      Comment


      • #4
        Thanks a lot. It's very helpful

        Comment


        • #5
          but in my page nothing show
          my bean config:
          Code:
           	<bean id="handlerMapping"
          		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
          		<property name="interceptors">
          			<list>
          				<bean
          					class="org.springmodules.validation.valang.javascript.taglib.ValangRulesExportInterceptor" />
          			</list>
          		</property>
          	</bean>
          
          	<bean id="profileController" class="com.mycompany.myproject.web.ProfileController">
          		<property name="profileManager" ref="profileManager" />
          		<property name="validators" ref="profileValidator" />
          	</bean>
          
          	<bean id="profileValidator" class="com.mycompany.myproject.domain.validators.ProfileValidator">
          		<property name="profileManager" ref="profileManager" />
          		<property name="valang">
          			<value>
          				<![CDATA[
          					{ name : ? HAS LENGTH : 'Profile name is required' : 'required.profileName' }
          					{ name : ? IS BLANK OR length(?) <= 30 : 'Profile name must be no longer than {0} characters' : 'proflieName.length': 30 }
          				]]>
          			</value>
          		</property>
          	</bean>
          taglibs.jsp
          HTML Code:
          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
          <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
          <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
          <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
          <%@ taglib uri="http://www.springmodules.org/tags/valang"
          	prefix="valang"%>
          profile.jsp
          HTML Code:
           <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          	pageEncoding="ISO-8859-1"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <%@ include file="/WEB-INF/jsp/taglibs.jsp"%>
          <html>
          <head>
          <valang:codebase includeScriptTags="true" fieldErrorsIdSuffix="_error"
          	globalErrorsId="global_error" />
          </head>
          <body>
          <form:form method="POST" action="add.htm" commandName="profile">
          	<valang:validate commandName="profile">
          					{ name : ? HAS LENGTH : 'Profile name is required' : 'required.profileName' }
          					{ name : ? IS BLANK OR length(?) <= 30 : 'Profile name must be no longer than {0} characters' : 'proflieName.length': 30 }
          	</valang:validate>
          	<spring:message code="name" />
          	<form:input path="name" />
          	<form:errors path="name" cssClass="errorBox" />
          	<input type="submit" />
          </form:form>
          </body>
          </html>
          my validator
          Code:
           */
          public class ProfileValidator extends ValangValidator {
          	private ProfileManager profileManager;
          	private final Log logger = LogFactory.getLog(getClass());
          
          	public void setProfileManager(ProfileManager profileManager) {
          		this.profileManager = profileManager;
          	}
          
          	@Override
          	public boolean supports(Class clazz) {
          		return Profile.class.isAssignableFrom(clazz);
          	}
          
          	@Override
          	public void validate(Object target, Errors errors) {
          		Profile profile = (Profile) target;
          		if (profileManager.isExist(profile)) {
          			errors.rejectValue("name", "exist.profileName",
          					"Profile name already exist");
          		}
          		super.validate(target, errors);
          	}
          }
          Not errors, but nothing show in my profile.jsp
          Why?

          Comment

          Working...
          X