Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC - Liferay - Validation (with @valid annotation)
    primarykey
    data
    text
    <p>I'm trying to use the "@Valid" annotations in a Controller with Spring MVC (and Liferay 6).</p> <p>I have "test-portlet.xml" :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt; &lt;context:component-scan base-package="com.test.sample.sample.ipc.test" /&gt; &lt;mvc:annotation-driven /&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; &lt;property name="prefix" value="/WEB-INF/view/test/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Portlet.xml :</p> <pre><code>&lt;portlet-app version="2.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"&gt; &lt;portlet&gt; &lt;portlet-name&gt;test&lt;/portlet-name&gt; &lt;portlet-class&gt;org.springframework.web.portlet.DispatcherPortlet&lt;/portlet-class&gt; &lt;init-param&gt; &lt;name&gt;contextConfigLocation&lt;/name&gt; &lt;value&gt; /WEB-INF/context/test-portlet.xml &lt;/value&gt; &lt;/init-param&gt; &lt;supports&gt; &lt;mime-type&gt;text/html&lt;/mime-type&gt; &lt;portlet-mode&gt;view&lt;/portlet-mode&gt; &lt;/supports&gt; &lt;portlet-info&gt; &lt;title&gt;Test&lt;/title&gt; &lt;/portlet-info&gt; &lt;/portlet&gt; &lt;/portlet-app&gt; </code></pre> <p>JavaBean.java (it's my "form") :</p> <pre><code>package com.test.sample.sample.ipc.test; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.NotEmpty; public class JavaBean { private String testName; public void setTestName(String testName) { this.testName = testName; } @NotEmpty(message = "{err.test.notEmpty}") @NotNull(message = "{err.test.notNull}") @Max(message = "{err.test.max}", value = 10) @Min(value = 1, message = "{err.test.min}") public String getTestName() { return testName; } } </code></pre> <p>And TestController.java :</p> <pre><code>package com.test.sample.sample.ipc.test; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.portlet.bind.annotation.ActionMapping; import com.test.auction.services.model.Test; import com.test.auction.services.service.TestLocalServiceUtil; @Controller @RequestMapping("VIEW") public class TestController { @RequestMapping public String doView(RenderRequest request, RenderResponse response) { return "test"; } @ActionMapping("addTest") public void addTestAction(@Valid JavaBean bean, BindingResult result, SessionStatus status, ActionRequest request, ActionResponse response) { try { Test test = TestLocalServiceUtil.addTest(bean.getTestName()); test.setName("test"); request.setAttribute("testAttr", "OK"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("testAttr", "ERROR"); } status.setComplete(); } } </code></pre> <p>The view (test.jsp) :</p> <pre><code>&lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt; &lt;%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %&gt; &lt;%@ page import="javax.portlet.PortletPreferences" %&gt; &lt;portlet:defineObjects/&gt; &lt;portlet:actionURL name="addTest" var="editURL"/&gt; &lt;c:out value="${testAttr}" /&gt; &lt;aui:form action="&lt;%= editURL %&gt;" name="addTest" method="post"&gt; &lt;aui:input label="Enter name of new Test Entity :" name="testName" type="text" value="" /&gt; &lt;aui:button type="submit" /&gt; &lt;/aui:form&gt; </code></pre> <p>Everything works as expected except for the validation of the JavaBean object. So, when I enter something in the text field, the JavaBean object is populated with it's value. If I enter nothing, for example, no error occurs or is visible in the "BindingResult" object.</p> <p>I tried putting the @valid annotations at the setter method or at the variable directly, but I get the same behavior.</p> <ul> <li>I have the library "hibernate-validator-annotation-processor-4.1.0.Final.jar" in the "lib" folder under web-inf.</li> </ul> <p>UPDATE : The @Min and @Max annotations should be used on integers, not string like I did!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload