开发者

Spring MVC - Liferay - Validation (with @valid annotation)

I'm trying to use the "@Valid" annotations in a Controller with Spring MVC (and Liferay 6).

I have "test-portlet.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: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">
    <context:component-scan base-package="com.test.sample.sample.ipc.test" />
    <mvc:annotation-driven  />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/test/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Portlet.xml :

<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">
    <portlet>
        <portlet-name>test</portlet-name>
        <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
        <init-param>
        <name>contextConfigLocation</name>
        <value>
            /WEB-INF/context/test-portlet.xml
        </value>
    </init-param>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
        <portlet-info>
            <title>Test</title>
        </portlet-info>       

    </portlet>     
</portlet-app>

JavaBean.java (it's my "form") :

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

And TestController.java :

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.servic开发者_开发技巧es.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();       
    }

}

The view (test.jsp) :

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@ page import="javax.portlet.PortletPreferences" %>

<portlet:defineObjects/>

<portlet:actionURL name="addTest" var="editURL"/>

<c:out value="${testAttr}" />

<aui:form action="<%= editURL %>" name="addTest" method="post">
    <aui:input label="Enter name of new Test Entity :" name="testName" type="text" value="" />
    <aui:button type="submit" />
</aui:form>

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.

I tried putting the @valid annotations at the setter method or at the variable directly, but I get the same behavior.

  • I have the library "hibernate-validator-annotation-processor-4.1.0.Final.jar" in the "lib" folder under web-inf.

UPDATE : The @Min and @Max annotations should be used on integers, not string like I did!


I made it work with heikkim's answer at Spring portlet mvc: @Valid does not seem to work

Basically, I replaced the mvc:annotation-driven line in test-portlet with :

<mvc:annotation-driven validator="validator" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator">
                <ref bean="validator"/>
            </property>
        </bean>
    </property>
</bean>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜