I'm very new in Java. So if I'm saying something nonsense then please suggest the correct way.
I'm trying to use spring MVC form validation using Java bean API validation. Seems I'm getting the error message in my controller. But, I need some model attribute to send to view along with the validation message and here I'm unable to pass it correctly. Please suggest me the correct way to do it.
Display Controller:
@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";
}
Form Submit Controller:
/**
* 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 view:
<%@ 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>
NOTE: By debugging I could see the error messages in form submit controller. But, unable to send to the views.
May be my question is similar to this post. But, here in my post the display and form processing controllers are different and these two controllers handle multiple URLs. I found difficult to apply the same solution in my case.