如何使用bean验证API获取验证错误消息以及模型属性

时间:2015-03-15 20:03:08

标签: java spring validation

我是Java的新手。所以,如果我说废话,那么请建议正确的方法。

我正在尝试使用Java bean API验证来使用spring MVC表单验证。好像我在控制器中收到错误信息。但是,我需要一些模型属性发送到查看以及验证消息,在这里我无法正确传递它。请建议我正确的方法。

显示控制器:

@RequestMapping(value = "/admin/student")
	public String student(Model model) throws ParseException {
		model.addAttribute("student", new Student());
		model.addAttribute("studentlist", this.studentService.getAll());
		model.addAttribute("statelist", this.stateService.getAll());
		model.addAttribute("courselist", this.courseService.getAllForDropDown());
		return "manage-student";
	}

表格提交控制人:

/**
	 * Adds or updates a student.
	 */
	@RequestMapping(value = {"/data-entry/student/add", "/data-entry/student/add/"}, method = RequestMethod.POST)
	public String addStudent(@Valid @ModelAttribute("student") Student s, BindingResult result, Map<String, Object> model) {

		if( ! result.hasErrors() ) {
			if( s.getId() == 0 ) {
				this.studentService.add(s);
			} else {
				this.studentService.update(s);
			}
			
			return "redirect:/admin/student";
		} else {
			return "forward:/admin/student";
		}
	}

JSP视图:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<style>
.error {
	color: red;
}
</style>

<div class="row">
     <div class="box">
		<div class="col-lg-12">
            <hr><h2 class="intro-text text-center">Manage Student</h2><hr>
        </div>
        <div class="col-md-12">
			<c:url var="addAction" value="/data-entry/student/add" ></c:url>
			
			<form:form action="${addAction}" commandName="student">
				<c:if test="${!empty student.firstName}">
					<form:hidden path="id" />
					<form:hidden path="ntContactID" />
				</c:if>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="firstName"><spring:message text="First Name"/></form:label>
						<form:input path="firstName" class="form-control" />
						<form:errors path="firstName" cssClass="error" />
					</div>
					<div class="form-group col-xs-5">
						<form:label path="lastName"><spring:message text="Last Name"/></form:label>
						<form:input path="lastName" class="form-control" />
						<form:errors path="lastName" cssClass="error" />
					</div>
				</div>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="DOB"><spring:message text="Date Of Birth"/></form:label>
						<div class="input-group date datetimepicker">
							<form:input path="DOB" class="form-control" />
							<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
							<form:errors path="DOB" cssClass="error" />
						</div>
					</div>
					<div class="form-group col-xs-5">
						<form:label path="DOA"><spring:message text="Date Of Admission"/></form:label>
						<div class="input-group date datetimepicker">
							<form:input path="DOA" class="form-control" />
							<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
							<form:errors path="DOA" cssClass="error" />
						</div>
					</div>
				</div>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="email"><spring:message text="Email"/></form:label>
						<form:input path="email" class="form-control" />
						<form:errors path="email" cssClass="error" />
					</div>
					<div class="form-group col-xs-5">
						<form:label path="password"><spring:message text="Password"/></form:label>
						<form:input path="password" class="form-control" />
					</div>
				</div>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="vcAddress1"><spring:message text="Address1"/></form:label>
						<form:input path="vcAddress1" class="form-control" />
					</div>
					<div class="form-group col-xs-5">
						<form:label path="vcAddress2"><spring:message text="Address2"/></form:label>
						<form:input path="vcAddress2" class="form-control" />
					</div>
				</div>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="vcPIN"><spring:message text="PIN"/></form:label>
						<form:input path="vcPIN" class="form-control" />
					</div>
					<div class="form-group col-xs-5">
						<form:label path="ntStateID"><spring:message text="State"/></form:label>
						<form:select path="ntStateID" items="${statelist}" class="form-control" />
					</div>
				</div>
				<div class="row">
					<div class="form-group col-xs-5">
						<form:label path="vcPhone"><spring:message text="Phone"/></form:label>
						<form:input path="vcPhone" class="form-control" />
					</div>
					<div class="form-group col-xs-5">
						<form:label path="courseID"><spring:message text="Course Name"/></form:label>
						<form:select path="courseID" items="${courselist}" class="form-control" />
					</div>
				</div>
				<div class="form-group">
					<c:if test="${!empty student.firstName}">
						<input type="submit" value="<spring:message text="Edit Student"/>" class="btn btn-default" />
					</c:if>
					<c:if test="${empty student.firstName}">
						<input type="submit" value="<spring:message text="Add Student"/>" class="btn btn-default" />
					</c:if>
				</div>
			</form:form>

			<h3>Student List</h3>
			<c:if test="${!empty studentlist}">
				<table class="tg">
				<tr>
					<th>First Name</th>
					<th>Last Name</th>
					<th>Date Of Birth</th>
					<th>Date Of Admission</th>
					<th>Course Name</th>
					<th>Email</th>
					<th>Phone</th>
					<th>Edit</th>
					<th>Delete</th>
				</tr>
				<c:forEach items="${studentlist}" var="student">
					<tr>
						<td>${student.firstName}</td>
						<td>${student.lastName}</td>
						<td><fmt:formatDate pattern="MM-dd-yyyy" value="${student.DOB}" /></td>
						<td><fmt:formatDate pattern="MM-dd-yyyy" value="${student.DOA}" /></td>
						<td>${student.courseName}</td>
						<td>${student.email}</td>
						<td>${student.vcPhone}</td>
						<td><a href="<c:url value='/admin/subject/edit/${student.id}' />" >Edit</a></td>
						<td><a href="<c:url value='/data-entry/subject/delete/${student.id}' />" >Delete</a></td>
					</tr>
				</c:forEach>
				</table>
			</c:if>
		</div>
     </div>
</div>

注意:通过调试,我可以在表单提交控制器中看到错误消息。但是,无法发送给观点。

可能是我的问题类似于post。但是,在我的帖子中,显示和表单处理控制器是不同的,这两个控制器处理多个URL。在我的案例中,我发现难以应用相同的解决方案。

0 个答案:

没有答案