2
我試圖在Spring MVC(和Liferay 6)中使用「@Valid」註釋。Spring MVC - Liferay - Validation(with @valid annotation)
我有 「測試的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方法或變量上,但我得到了相同的行爲。
- 我在web-inf下的「lib」文件夾中有「hibernate-validator-annotation-processor-4.1.0.Final.jar」庫。
更新:@Min和@Max註釋應該用於整數,而不是像我這樣的字符串!