Spring MVC表单验证未验证

时间:2019-03-30 21:07:46

标签: spring-mvc spring-validator

尝试将MVC Spring Validation应用于我的Web项目。我认为我已经正确配置了所有内容,但是我的表单尚未通过验证。

我没有使用Maven或Gradle。相反,我将我的教程告诉我需要的jar文件添加到构建路径中。

jar文件为: 验证API-1.1.0.Final.jar hibernate-validator-5.0.1.Final.jar

我正在关注本教程:https://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api

我的config.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: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>

我的模特:

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;
        }
}

我的观点:

<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>

我的控制器:

@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";
        }
    }

在尝试提交表单时,BindingResult对象不包含任何错误,当我将字段保留为空时,它应该包含任何错误。因此,if(result.hasErrors())不会触发,并且由于尝试插入空值而出现数据库异常。

1 个答案:

答案 0 :(得分: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

从目录添加jar- hibernate-release-5.4.2.Final\lib\required

下载休眠验证器- https://sourceforge.net/projects/hibernate/files/hibernate-validator/6.0.16.Final/hibernate-validator-6.0.16.Final-dist.zip/download

添加外部存在的罐子: hibernate-validator-6.0.16.Final\dist

记住:将所有罐子添加到类路径中。

现在,我提供一个示例代码来演示:

spring.xml或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>

我认为这可以解决您的问题。