0

Trying to apply MVC Spring Validation to my web project. I think I have everything configured properly, but my form is not being validated.

I am not using Maven or Gradle. Rather, I am including the jar files I was told I needed by my tutorial in my build path.

The jar files are: validation-api-1.1.0.Final.jar hibernate-validator-5.0.1.Final.jar

I am following this tutorial: https://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api

My config.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <mvc:annotation-driven />
</beans>

My Model:

package bl;

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

public class VendorValidation {

        @NotEmpty
        @Size(min = 1, message = "Field requires an entry")
        private String vname;       
        @NotEmpty
        private String vphone;  
        @NotEmpty
        private String vemail;  
        @NotEmpty
        private String vcity;   
        @NotEmpty
        private String vstate;  
        @NotEmpty
        private String vcountry;    
        @NotEmpty
        private String vzipcode;    
        @NotEmpty
        private String vtimezone;
        public String getVname() {
            return vname;
        }
        public void setVname(String vname) {
            this.vname = vname;
        }
        public String getVphone() {
            return vphone;
        }
        public void setVphone(String vphone) {
            this.vphone = vphone;
        }
        public String getVemail() {
            return vemail;
        }
        public void setVemail(String vemail) {
            this.vemail = vemail;
        }
        public String getVcity() {
            return vcity;
        }
        public void setVcity(String vcity) {
            this.vcity = vcity;
        }
        public String getVstate() {
            return vstate;
        }
        public void setVstate(String vstate) {
            this.vstate = vstate;
        }
        public String getVcountry() {
            return vcountry;
        }
        public void setVcountry(String vcountry) {
            this.vcountry = vcountry;
        }
        public String getVzipcode() {
            return vzipcode;
        }
        public void setVzipcode(String vzipcode) {
            this.vzipcode = vzipcode;
        }
        public String getVtimezone() {
            return vtimezone;
        }
        public void setVtimezone(String vtimezone) {
            this.vtimezone = vtimezone;
        }
}

My View:

<form:form method="post" id="va-form" action="insertVendor" modelAttribute="vendorValidation">
                <div class="form-group">
                    <label>Vendor Name</label> <form:input type="text" path="vname" class="form-control"
                        id="nameForm" />
                </div>

...

</form:form>

My Controller:

@RequestMapping(value = "vendorForm", method = RequestMethod.GET)
    public String formView(ModelMap map, HttpServletRequest request) {

        VendorValidation vendorValidation = new VendorValidation();
        map.put("vendorValidation", vendorValidation);

    return "vendorForm";
}

RequestMapping(value = "/insertVendor", method = RequestMethod.POST)
    public String insertVendor(@Valid @ModelAttribute("vendorValidation") VendorValidation vendorValidation, BindingResult result,
            HttpServletRequest request, ModelMap model) {
        System.out.println("Found form errors: " + result.hasErrors());
        if (result.hasErrors())
        {
            return "vendorForm";
        }
        else
        {
             // database logic
            return "vendormanagement";
        }
    }

The BindingResult object does not contain any errors when attempting to submit the form and it should when I leave my fields empty. So the if (result.hasErrors()) does not fire and I get a database exception for trying to insert null values.

1
  • are you using eclipse ?? Commented Mar 31, 2019 at 6:50

1 Answer 1

0

Download the Spring framework "-dist.zip" from here - https://repo.spring.io/release/org/springframework/spring/

Download hibernate ORM zip - https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.4.2.Final/hibernate-release-5.4.2.Final.zip/download

Add jar from directory - hibernate-release-5.4.2.Final\lib\required

Download hibernate validator - https://sourceforge.net/projects/hibernate/files/hibernate-validator/6.0.16.Final/hibernate-validator-6.0.16.Final-dist.zip/download

Add jars present outside : hibernate-validator-6.0.16.Final\dist

Remember : Add all the jars to the classpath.

Now, I'm providing a sample code to demonstrate :

spring.xml or application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan
        base-package="com.demo" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Add custom message resources -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="resources/message"></property>
    </bean>
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>project</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

I think that this will solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.