1

I need some help. I have a trouble working on my pet project. It's simple crud app based on spring mvc. The trouble is that I can't get error messages when input validation fails in my profile.jsp. I have mostly the same code in my signup.jsp but there all works fine.

There is my controller methods:

@RequestMapping(value = "/profile", method = RequestMethod.GET)
    public String getUserProfile(Model model) {
        model.addAttribute("userDetailsForm", new UserDetailsFormDTO());
        model.addAttribute("passwordForm" ,new PasswordFormDTO());
        model.addAttribute("profile", getCurrentUser());
        return "profile";
    }

@RequestMapping(value = "/profile/details/change", method = RequestMethod.POST)
public String changeUserDetails(@ModelAttribute("userDetailsForm") @Valid UserDetailsFormDTO form,
        BindingResult result) {
    if(result.hasErrors()){
        result.getAllErrors().forEach(log::debug);
        return "redirect:/profile";
    }
    userService.changeUserDetails(getCurrentUser(), form);
    return "redirect:/profile?success=details";
}

I'm using JSR 303 Validation:

package org.crud.dto;

import java.io.Serializable;

import javax.validation.constraints.Pattern;

import org.crud.validation.InputValidator;
import org.hibernate.validator.constraints.NotEmpty;

public class UserDetailsFormDTO implements Serializable{

    private static final long serialVersionUID = -7603395840362468805L;

    @NotEmpty(message="{error.null_form_value}")
    @Pattern(regexp=InputValidator.FIRSTNAME_PATTERN, message="{error.name_invalid}")
    private String firstName;

    @NotEmpty(message="{error.null_form_value}")
    @Pattern(regexp=InputValidator.LASTNAME_PATTERN, message="{error.name_invalid}")
    private String lastName;

    public UserDetailsFormDTO() {}

    public UserDetailsFormDTO(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

In configuration files I have next beans declared:

<bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"
        p:validationMessageSource-ref="messageSource" />

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

Also have two .properties files with my labels and error codes on my classpath: messages_ru and messages_en.

My profile.jsp code fragment:

<div id="user-details-upd" class="panel panel-info">
    <div class="panel-heading">
        <h3 class="panel-title">Update user details</h3>
    </div>
    <div class="panel-body">
        <div class="container-fluid">

            <form name="userDetailsForm" class="form-horizontal" role="form" method="POST" action="profile/details/change">
                <spring:bind path="userDetailsForm"></spring:bind>
                <div class="form-group">
                    <div class="row">
                        <label for="fname-input" class="col-md-offset-2 col-md-2 control-label">First name:</label>
                        <div class="col-md-5">
                            <spring:message code="label.fname_placeholder" var="fNameHolder" />
                            <spring:bind path="userDetailsForm.firstName">
                                <input type="text" value="${profile.getFirstName()}" name="<c:out value="${status.expression}"/>" class="form-control" placeholder="${fNameHolder}" required>
                                <c:if test="${status.error}">
                                    <c:forEach items="${status.errorMessages}" var="error">
                                        <small class="text-danger"> <c:out value="${error}" />
                                        </small>
                                    </c:forEach>
                                </c:if>
                            </spring:bind>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="row">
                        <label for="lname-input" class="col-md-offset-2 col-md-2 control-label">Last name:</label>
                        <div class="col-md-5">
                            <spring:message code="label.lname_placeholder" var="lNameHolder" />
                            <spring:bind path="userDetailsForm.lastName">
                                <input type="text" value="${profile.getLastName()}" name="<c:out value="${status.expression}"/>" class="form-control" placeholder="${lNameHolder}" required>
                                <c:if test="${status.error}">
                                    <c:forEach items="${status.errorMessages}" var="error">
                                        <small class="text-danger"> <c:out value="${error}" />
                                        </small>
                                    </c:forEach>
                                </c:if>
                            </spring:bind>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="row">
                        <div class="col-md-offset-4 col-md-5">
                            <button type="submit" value="Submit" class="btn btn-success">
                                <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Update
                            </button>
                        </div>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

And message from debug when I submit incorrect data in lastName input:

2015-08-07 18:52:29 DEBUG UserController:? - Field error in object 'userDetailsForm' on field 'lastName': rejected value [jh]; codes [Pattern.userDetailsForm.lastName,Pattern.lastName,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userDetailsForm.lastName,lastName]; arguments []; default message [lastName],[Ljavax.validation.constraints.Pattern$Flag;@657f42ee,([A-Z�-�][a-zA-Z�-�]*)([\s\'-][A-Z�-�][a-z�-�]*)*]; default message [Name must contain only characters and first letter must be in uppercase]

I can't understand, why status.errorMessages is empty when BindingResult has got errors?

Any help or suggestions are appreciated. Thanks in advance

  • See my working code here: http://stackoverflow.com/questions/29039116/spring-mvc-validation-post-redirect-get-partial-updates-optimistic-concurren/29039117#29039117 . Basically you want to put your binding result into a flash attribute before redirect – Neil McGuigan Aug 07 '15 at 18:13
  • @NeilMcGuigan my error messages are lost when redirect occurs, am I right? – Ilya Potapchuk Aug 08 '15 at 11:05
  • You might need to use Springs form input tags – Neil McGuigan Aug 08 '15 at 18:29

1 Answers1

1

You are using redirects within the request mapping block of your controller. The redirect is a header sent to the browser. The browser initiates the redirect, consequently you get a totally new request from the browser and because http is stateless, you lose anything stored in that previous request/response, such as the BindingResult.

remove the redirects and use a string to forward to the jsp page. You can use the internalviewresolver for this

Ken
  • 11
  • 1