3

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

user237865
  • 1,250
  • 4
  • 19
  • 41

3 Answers3

2

Spring roo use the spring tag form:errors which will print any error messages associated with that field.

So all you need to do is generate the error message is a manner that it will get picked up by the spring form:errors tag.

There are many ways to do the validation, the simplest way to start with is to do it in the controller itself.

So your code would change to:

try {
    vendorService.saveVendor(vendor);
    uiModel.asMap().clear();
} catch(Exception e) {
    uiModel.addAttribute("vendor", vendor);
    bindingResult.rejectValue("name", "vendor.name.duplicate");
    addDateTimeFormatPatterns(uiModel);
    return "vendors/create";
}

Note: vendor.name.duplicate is a messages property which you must define in the WEB-INF/i18n/messages.properties

Solubris
  • 3,603
  • 2
  • 22
  • 37
  • Thanks for the response. Could you please tell me how to do it? "need to do is generate the error message is a manner that it will get picked up by the spring form:errors tag." – user237865 Mar 23 '13 at 17:11
1

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>
niks75
  • 73
  • 7
  • I thought of splitting the bounty with this answer, but then I realized that there is no such option... I'm sorry. Both the answers helped me. – user237865 Mar 25 '13 at 10:24
0

There are a couple of ways of approaching this but I believe I would put an @UNIQUE validator on your entity's vendor attribute. I believe you can add the constraint by issuing another jpa command, identical to the one initially used to create the vendor field but with the addition of --unique option.

http://static.springsource.org/spring-roo/reference/html/command-index.html#command-index-field-commands

Slartibartfast
  • 1,605
  • 2
  • 16
  • 23
  • Thanks for the response. Yes, I could have added the validator, but here I'm looking for how to send an attribute or a value to the view (apache tile), this is just a use case. Thanks. – user237865 Mar 14 '13 at 17:25
  • You can put a getter on the object that returns a string and call it like a property in the page. – Slartibartfast Mar 18 '13 at 18:54
  • Could you please explain me that (if possible with code), thanks. – user237865 Mar 21 '13 at 12:08