I'm using Spring roo, and in one of the controllers I'm setting the model attribute "error" with a string as below:
//uiModel.addAttribute("error", "Duplicate name for Vendor");
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid Vendor vendor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
uiModel.addAttribute("vendor", vendor);
addDateTimeFormatPatterns(uiModel);
return "vendors/create";
}
try {
vendorService.saveVendor(vendor);
uiModel.asMap().clear();
} catch(Exception e) {
uiModel.addAttribute("vendor", vendor);
uiModel.addAttribute("error", "Duplicate name for Vendor");
addDateTimeFormatPatterns(uiModel);
return "vendors/create";
}
return "redirect:/vendors/" + encodeUrlPathSegment(vendor.getId().toString(), httpServletRequest);
}
Now my question is how do I display the error (if not null) in the create page, which is as below for now.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<form:create id="fc_domain_Vendor" modelAttribute="vendor" path="/vendors" render="${empty dependencies}" z="MGZPL+gO+CDX6M4iRO/z/qRfnJI=">
<field:input field="name" id="c_domain_Vendor_name" required="true" z="s+3hs8xXpSZ71RoD0ktXy0BnjS0="/>
<field:input field="email" id="c_domain_Vendor_email" validationMessageCode="field_invalid_email" z="+4rIdPGArWhHQlrFG/1N6yrKKno="/>
<field:input field="mobile" id="c_domain_Vendor_mobile" max="16" z="kgM5Z9jJ6xW9BxiPPB4Ipz0TUKg="/>
</form:create>
<form:dependency dependencies="${dependencies}" id="d_domain_Vendor" render="${not empty dependencies}" z="hLv7c7K8OOSRrBJKgKuw9H1+GvA="/>
</div>
Thanks in advance
You can use the following snippet to get the error string and then use an alert mechanism to display the error message:
<c:if test="${not empty error}">
<c:out value="${error}"/>
</c:if>