我正在尝试在带有Spring MVC(和Liferay 6)的Controller中使用“@Valid”注释。
我有“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(这是我的“形式”):
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;
}
}
和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.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();
}
}
视图(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>
除了验证JavaBean对象之外,一切都按预期工作。因此,当我在文本字段中输入内容时,JavaBean对象将填充其值。例如,如果我没有输入任何内容,则“BindingResult”对象中不会发生错误或显示错误。
我尝试将@valid注释直接放在setter方法或变量上,但我得到了同样的行为。
更新:@Min和@Max注释应该用于整数,而不是像我一样使用字符串!
答案 0 :(得分:3)
我使用了heikkim的答案 Spring portlet mvc: @Valid does not seem to work
基本上,我将test-portlet中的mvc:annotation-driven行替换为:
<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>