سؤال

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

هل كانت مفيدة؟

المحلول 2

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>

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top