Spring MVC表单验证未经验证

问题描述 投票:0回答:1

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

我没有使用Maven或Gradle。相反,我在构建路径中包含了我的教程所需的jar文件。

jar文件是:validation-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())不会触发,并且我尝试插入空值时会收到数据库异常。

spring-mvc spring-validator
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

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

下载hibernate验证器 - 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

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

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

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>

veb.hml:

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

我认为这将解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.