Spring MVC验证抛出ConstraintViolationException

时间:2016-11-06 21:13:21

标签: forms hibernate validation spring-mvc

我想验证bean类Articolo,它是模型类NewEditArticle的一个属性,其实例在控制器中创建并传递给视图ArticleManager.jsp。

此视图是在SQL数据库中插入,更新或删除文章的表单。

我尝试实现基于注释的验证系统,但由于某种原因它不起作用:例如,如果我提交表单而没有在bean类中填充标记为NotEmpty / NotNull的字段之一,系统抛出与验证相关的异常,但没有从BindingResult中捕获任何内容。

据我从错误消息中了解,验证排序有效,但result.hasErrors()不捕获任何内容,因此它不会返回显示错误消息的表单。

知道问题出在哪里以及如何解决问题?

N.B:我的配置中已经有mvc:annotation-driven

Bean类

@Entity
@Table (name="Articolo")
public class Articolo {

@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="ID")
@NotNull (message="error")
private int id;

@Column (name="Ristorante")
@NotEmpty (message="error")
private String ristorante;

@Column (name="Data")
@NotEmpty (message="error")
private String data;

@Column (name="Articolo")
@NotEmpty (message="error")
private String articolo;

@Column (name="IDArea")
@NotNull (message="error")
private int IDArea;

@Column (name="IDCucina")
@NotNull (message="error")
private int IDCucina;

@Column (name="IDPrezzo")
@NotNull (message="error")
private int IDPrezzo;

@Column (name="IDVoto")
@NotNull (message="error")
private int IDVoto;

@Column (name="Foto")
private String foto;


//CONSTRUCTORS, GETTERS AND SETTERS

模型类

public class NewEditArticolo {

//ATTRIBUTES
private Articolo articolo;
private List<Area> ListaArea;
private List<Cucina> ListaCucina;
private List<Prezzo> ListaPrezzo;
private List<Voto> ListaVoto;
private List<String> ListaImg;




//METHODS

//CONSTRUCTOR

public NewEditArticolo () {

}

//create Article model based on ID number
//if ID=0 (new Article), creates empty model
public NewEditArticolo(int ID) throws SQLException {

    //call DAOArticolo.select only if article already exists
    if (ID != 0) {
        DAOArticolo DAOart = new DAOArticolo();
        articolo = DAOart.select(ID);
    }

    //return list of all objects from table Area
    DAOArea DAOa = new DAOArea();
    ListaArea = DAOa.getArea();

    //return list of all objects from table Cucina
    DAOCucina DAOc = new DAOCucina();
    ListaCucina = DAOc.getCucina();

    //return list of all objects from table Prezzo
    DAOPrezzo DAOp = new DAOPrezzo();
    ListaPrezzo = DAOp.getPrezzo();

    //return list of all objects from table Voto
    DAOVoto DAOv = new DAOVoto();
    ListaVoto = DAOv.getVoto();

    ListaImg = new ArrayList<String>();


    // create array containing the path of all files in folder img
    File folder = new File("C:/Users/Ale/workspace/SpringMVCBlog/WebContent/resources/img");
    File[] arrayImg = folder.listFiles();
    // get the name of each file in the array and add it to ListaImg
    for (File f : arrayImg) {
        String fileName=f.getName();
        ListaImg.add(fileName);
    }

}

//GETTERS AND SETTERS

Servlet类

    @Controller
 public class ArticleManagerController {

@RequestMapping (value="/ArticleManager", method= RequestMethod.GET)

//creates model and pass it to view ARTICLEMANAGER.JSP
public ModelAndView createArticleManager(@RequestParam(value = "IDarticolo") int IDarticolo) {

    ModelAndView model=new ModelAndView("ArticleManager");

    //get article based on ID number from URL
    //if ID=0, creates empty model (new article)

        try {
            NewEditArticolo nea = new NewEditArticolo(IDarticolo);
            model.addObject("nea", nea);

        }

        catch (SQLException e) {
            System.out.println("failed to generate model for article with ID " + IDarticolo);
            e.printStackTrace();
        }


    return model;

}

// insert, edit or delete article in DB
@RequestMapping (value="/ArticleManager", method=RequestMethod.POST)

public String editArticle ( Model model,
                            @Valid @ModelAttribute (value="nea") NewEditArticolo nea, //create NewEditArticolo object, autowire attributes from ArticleManager.jsp, add to model and validate
                            BindingResult result, //collect validation errors
                            @RequestParam (value="submit") String submit){ //get input value from ArticleManager.jsp

    Articolo articolo1=nea.getArticolo();

    DAOArticolo daoArt = new DAOArticolo();

    //if validation fails, return form to display validation errors
    if (result.hasErrors()) {
        System.out.println("VALIDATION FAILED");
        return "ArticleManager";
    }

    else {
        System.out.println("VALIDATION WAS SUCCESFULL");
    }


    switch (submit) {

    case "SUBMIT": {

        // INSERT new article
        if (articolo1.getId() == 0) {

            try {
                daoArt.insert(articolo1);
                System.out.println("new article was created");
                return "redirect:/admin?insert";

            } catch (Exception  e) {
                System.out.println("failed to create new article");
                e.printStackTrace();
            }
        }

        // UPDATE article
        else {

            try {
                daoArt.update(articolo1);
                System.out.println("article was updated");
                return "redirect:/admin?update"; 

            } catch (Exception  e) {
                System.out.println("failed to update article");
                e.printStackTrace();
            }
        }

        break;
    }

    //DELETE article
    case "DELETE ARTICLE": {

        try {
            daoArt.delete(articolo1);
            System.out.println("article was deleted");
            return "redirect:/admin?delete"; 

        } catch (Exception  e) {
            System.out.println("failed to delete article");
            e.printStackTrace();
        }

        break;
    }

    }

    return submit;

}

}

JSP

<t:ADMIN>
<!-- populates the form using the object nea (NewEditArticolo) from servlet ArticleManagerController -->
<!-- the parameteres are then passed back to the servlet ArticleManagerController -->
<form:form modelAttribute="nea" action="${pageContext.request.contextPath}/ArticleManager" method="POST">

    <table border="0">
        <tr>
            <!-- inputs the ID of the article in the cell; if the article was created with ID 0 (new article), the value is 0 -->
            <c:choose>
                <c:when test="${nea.articolo.id>0}">
                    <td>Article ID</td>
                    <td><form:input path="articolo.id" size="4" placeholder="ID articolo"
                        value="${nea.articolo.id}" readonly="readonly" /></td>
                </c:when>
                <c:otherwise>
                    <td>Article ID</td>
                    <td><form:input path="articolo.id" size="4" placeholder="ID articolo"
                        value="0" readonly="readonly" /></td>
                </c:otherwise>
            </c:choose>
            <td><form:errors path="articolo.id"/></td>
        </tr>

        <tr>
            <td>Restaurant</td>
            <td><form:input path="articolo.ristorante" size="50"
                placeholder="Insert restaurant name"
                value="${nea.articolo.ristorante}" /></td>
            <td><form:errors path="articolo.id"/></td>
        </tr>

        <tr>
            <td><label>Location</label></td>
            <td><form:select path="articolo.IDArea">
                    <!-- for each object in nea.listArea, checks the ID and adds the name to the select menu
                    if the ID of object in listArea matches the IDArea of the article, it is set as selected -->
                    <c:forEach var="area" items="${nea.listaArea}">
                        <c:choose>
                            <c:when test="${area.ID==nea.articolo.IDArea}">
                                <form:option value="${area.ID}" selected="selected">
                                    ${area.nome}</form:option>>
                            </c:when>
                            <c:otherwise>
                                <form:option value="${area.ID}">${area.nome}</form:option>>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
            </form:select></td>
            <td><form:errors path="articolo.IDArea"/></td>
        </tr>
        <!-- for each object in nea.listCucina, checks the ID and adds the name to the select menu
        if the ID of object in listCucina matches the IDCucina of the article, it is set as selected -->
        <tr>
            <td><label> Cuisine</label></td>
            <td><form:select path="articolo.IDCucina">
                    <c:forEach var="cucina" items="${nea.listaCucina}">
                        <c:choose>
                            <c:when test="${cucina.ID==nea.articolo.IDCucina}">
                                <form:option value="${cucina.ID}" selected="selected">
                                    ${cucina.nome}</form:option>
                            </c:when>
                            <c:otherwise>
                                <form:option value="${cucina.ID}">${cucina.nome}</form:option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
            </form:select></td>
            <td><form:errors path="articolo.IDCucina"/></td>
        </tr>
        <!-- for each object in nea.listPrezzo, checks the ID and adds the name to the select menu
        if the ID of object in listPrezzo matches the IDPrezzo of the article, it is set as selected -->
        <tr>
            <td><label> Price </label></td>
            <td><form:select path="articolo.IDPrezzo">
                    <c:forEach var="prezzo" items="${nea.listaPrezzo}">
                        <c:choose>
                            <c:when test="${prezzo.ID==nea.articolo.IDPrezzo}">
                                <form:option name="IDPrezzo" value="${prezzo.ID}"
                                    selected="selected">${prezzo.nome}</form:option>
                            </c:when>
                            <c:otherwise>
                                <form:option name="IDPrezzo" value="${prezzo.ID}">
                                    ${prezzo.nome}</form:option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
            </form:select></td>
            <td><form:errors path="articolo.IDPrezzo"/></td>
        </tr>

        <tr>
            <td>Website</td>
            <td><input name="website" size="50"
                placeholder="insert website address"}></td>
        </tr>

        <tr>
            <td>Article date</td>
            <td><form:input path="articolo.data" placeholder="aaaa/mm/dd" size="12"
                value="${nea.articolo.data}"></form:input>
                <td><form:errors path="articolo.data"/></td>
        </tr>
    </table>

    <!-- textbox -->
    <form:textarea path="articolo.articolo"/>
    <td><form:errors path="articolo.articolo"/></td>

    <table border="0">
        <tr>
            <td><label> Stars </label></td>

            <!-- for each object in nea.listVoto, checks the ID and adds the name to the select menu -->
            <!-- if the ID of the object in listVoto matches the IDVoto of the article, it is set as selected -->
            <td> <form:select path="articolo.IDVoto">
                    <c:forEach var="voto" items="${nea.listaVoto}">
                        <c:choose>
                            <c:when test="${voto.ID==nea.articolo.IDVoto}">
                                <form:option value="${voto.ID}" selected="selected">
                                    ${voto.nome}</form:option>>
                            </c:when>
                            <c:otherwise>
                                <form:option value="${voto.ID}">${voto.nome}</form:option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
            </form:select> </td>

            <td><form:errors path="articolo.IDVoto"/></td>
        </tr>

        <tr>
            <td><label>Picture</label></td>

            <td><form:select path="articolo.foto">
                    <c:forEach var="img" items="${nea.listaImg}">
                        <c:choose>
                            <c:when test="${img==nea.articolo.foto}">
                                <form:option value="img" selected="selected">${img}</form:option>
                            </c:when>
                            <c:otherwise>
                                <form:option value="img">${img}</form:option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
            </form:select></td>

            <td><form:errors path="articolo.foto"/></td>
        </tr>

        <tr>
            <td><label>CURRENT PICTURE</label></td>
        </tr>
        <tr>
            <td><img
                src="${pageContext.request.contextPath}/resources/img/${nea.articolo.foto}"
                alt="no picture" height=150 width=300></img></td>
        </tr>

        <tr>
            <td><input type="submit" value="SUBMIT" name="submit"></td>
        </tr>
        <tr>
            <td><input type="submit" value="DELETE ARTICLE" name="submit"></td>
        </tr>
    </table>
</form:form>

<!-- button BACK moves back to view admin.jsp-->
<form action="${pageContext.request.contextPath}/admin">
<table>
        <tr>
            <td><input type="submit" value="BACK"></td>
        </tr>
</table>
</form>

错误消息(在这种情况下,我尝试更新文章,将ristorante字段留空)

javax.validation.ConstraintViolationException: Validation failed for classes [beans.Articolo] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='error', propertyPath=ristorante, rootBeanClass=class beans.Articolo, messageTemplate='error'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:140)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:88)
    at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:244)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:118)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1397)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:473)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3133)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2370)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
    at DAO.DAOArticolo.update(DAOArticolo.java:105)
    at controllers.ArticleManagerController.editArticle(ArticleManagerController.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

我的POM文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>SpringMVCBlog</groupId>
  <artifactId>SpringMVCBlog</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

 <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- spring-context which provides core functionality -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
        programming implementation allowing you to define -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
        Spring’s model-view-controller (MVC) and REST Web Services implementation 
        for web applications -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <!-- The spring-web module provides basic web-oriented integration features 
        such as multipart file upload functionality and the initialization of the 
        IoC container using Servlet listeners and a web-oriented application context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <!-- http://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>

    <!-- validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.4.Final</version>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <!-- JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- HIBERNATE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.1.Final</version>
    </dependency>


    <!-- Java Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

</dependencies>

</project>

2 个答案:

答案 0 :(得分:0)

您似乎没有配置MessageValidation.properties,因此您需要将MessageValidation.properties(或相关配置文件)添加到类路径中以获取并填充错误消息,您可以查看{ {3}}

答案 1 :(得分:0)

最终,我在这里找到了解决方案: JSR-303 @Valid annotation (nested object) not working

在NewEditArticle类的Articolo字段中添加@Valid注释就足够了,验证就开始了。