#13 演示Spring MVC结合Hibernate Valdiator的客户端表单验证
This commit is contained in:
@@ -133,6 +133,11 @@
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
<!-- WEB end -->
|
||||
|
||||
<!-- SOAP begin -->
|
||||
|
||||
+3
@@ -14,6 +14,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.codehaus.jackson.annotate.JsonIgnore;
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import org.springside.modules.utils.Collections3;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -36,6 +37,7 @@ public class User extends IdEntity {
|
||||
|
||||
private List<Role> roleList = Lists.newArrayList(); //有序的关联对象集合
|
||||
|
||||
@NotBlank
|
||||
public String getLoginName() {
|
||||
return loginName;
|
||||
}
|
||||
@@ -69,6 +71,7 @@ public class User extends IdEntity {
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
@NotBlank
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
+9
-1
@@ -2,9 +2,12 @@ package org.springside.examples.showcase.common.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -34,7 +37,12 @@ public class UserDetailController {
|
||||
}
|
||||
|
||||
@RequestMapping(value = "save/{id}")
|
||||
public String save(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
|
||||
public String save(@Valid @ModelAttribute("user") User user, BindingResult bindingResult, Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return updateForm(model);
|
||||
}
|
||||
|
||||
accountManager.saveUser(user);
|
||||
redirectAttributes.addFlashAttribute("message", "保存用户成功");
|
||||
return "redirect:/common/user/";
|
||||
|
||||
@@ -24,4 +24,6 @@
|
||||
<property name="prefix" value="/WEB-INF/views/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
|
||||
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
|
||||
</beans>
|
||||
|
||||
@@ -6,15 +6,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>综合演示用例</title>
|
||||
<script src="${ctx}/static/jquery-validation/1.9.0/jquery.validate.min.js" type="text/javascript"></script>
|
||||
<script src="${ctx}/static/jquery-validation/1.9.0/messages_cn.js" type="text/javascript"></script>
|
||||
<link href="${ctx}/static/jquery-validation/1.9.0/validate.css" type="text/css" rel="stylesheet" />
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$("#inputForm").validate();
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@@ -25,11 +17,13 @@
|
||||
<legend>管理用户</legend>
|
||||
<div>
|
||||
<label for="loginName" class="field">登录名:</label>
|
||||
<input type="text" id="loginName" name="loginName" size="40" value="${user.loginName}" class="required"/>
|
||||
<input type="text" id="loginName" name="loginName" size="40" value="${user.loginName}"/>
|
||||
<form:errors path="loginName" cssClass="error"/>
|
||||
</div>
|
||||
<div>
|
||||
<label for="name" class="field">用户名:</label>
|
||||
<input type="text" id="name" name="name" size="40" value="${user.name}" class="required"/>
|
||||
<input type="text" id="name" name="name" size="40" value="${user.name}"/>
|
||||
<form:errors path="name" cssClass="error"/>
|
||||
</div>
|
||||
<div>
|
||||
<label for="plainPassword" class="field">密码:</label>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
label.error {
|
||||
label.error,span.error{
|
||||
background:url("images/unchecked.gif") no-repeat 0px 0px;
|
||||
padding-left: 16px;
|
||||
padding-bottom: 2px;
|
||||
font-weight: bold;
|
||||
color: #EA5200;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user