我正在學習如何使用本教程進行簡單的表單驗證: http://www.raistudies.com/spring/spring-mvc/form-validation-spring-mvc-3-hibernate-validator-jsr-303/Spring MVC 3表單驗證註釋被忽略?
的問題是,當我輸入無效數據(空白字段),對用戶沒有任何錯誤的任何地方。 result.hasErrors()總是產生false。
我產生以下代碼:
IndexController.java:
package pl.aadamczyk.springtest.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pl.aadamczyk.springtest.beans.LoginBean;
@Controller
@RequestMapping("/index")
public class IndexController
{
@RequestMapping(method = RequestMethod.GET)
public String getIndex(ModelMap model)
{
LoginBean loginBean = new LoginBean();
model.addAttribute("loginBean", loginBean);
return "index";
}
}
+ LoginController.java:
package pl.aadamczyk.springtest.controllers;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pl.aadamczyk.springtest.beans.LoginBean;
import pl.aadamczyk.springtest.validators.LoginValidator;
@Controller
@RequestMapping("/login")
public class LoginController
{
private LoginValidator loginValidator;
private LoginValidator getValidator()
{
return loginValidator;
}
@Autowired
public void setValidator(LoginValidator loginValidator)
{
this.loginValidator = loginValidator;
}
@RequestMapping(method= RequestMethod.POST)
public String login(@Valid @ModelAttribute(value="loginBean") LoginBean loginBean,
ModelMap model,
BindingResult result)
{
System.out.println(result.hasErrors()); // always false
loginValidator.validate(loginBean, result);
return "redirect:index.html";
}
}
+ LoginValidator.java
package pl.aadamczyk.springtest.validators;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import pl.aadamczyk.springtest.beans.LoginBean;
@Component
public class LoginValidator implements Validator
{
@Override
public boolean supports(Class<?> type)
{
return LoginBean.class.isAssignableFrom(type);
}
@Override
public void validate(Object o, Errors errors)
{
}
}
LoginBean.java:
<form:form method="post" action="login.html" modelAttribute="loginBean">
<table>
<tr>
<td>Login:</td>
<td><form:input path="username"/></td>
<td><form:errors path="username"/></td>
</tr>
<tr>
<td>Password</td>
<td><form:input path="password"/></td>
<td><form:errors path="password"/></td>
</tr>
<tr>
<td colspan="3"><button type="submit">Login</button></td>
</tr>
</table>
</form:form>
調度員的servlet
重要內容:
<context:annotation-config />
<context:component-scan base-package="pl.aadamczyk.springtest"/>
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
pom.xml的依賴關係:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-struts</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.ga</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
package pl.aadamczyk.springtest.beans;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
public class LoginBean
{
@NotEmpty(message="Username is mandatory.")
@Length(min=3,max=25,message="Username must be between 3 and 25 characters length")
private String username;
@NotEmpty(message="Password field is mandatory.")
@Length(min=3,max=25,message="Password must be between 5 and 25 characters length")
private String password;
// getters+setters skipped
}
的index.jsp的部分
你確定在編譯之後你的類路徑中有hibernate-validator嗎? –
我已經將hibernate-validator-4.1.0.Final.jar添加到WEB-INF/lib文件夾中,無需更改。 Hibernate-validator-3.0.0.ga.jar在Mavens的依賴中。我在第一篇文章中包含了我的pom.xml。 –
好的,我已經包含了ant-apache-log4j-1.6.5,hibernate-validator-4.3.0.Final,slf4j-api-1.6.6,slf4j-log4j12-1.6.6到我的依賴項,它似乎工作正常現在。另外,我必須將BindingResult結果更接近LoginBean loginBean參數。 result.hasErrors()現在返回true,但html中仍然沒有消息。 –