Spring MVC Form: Errors Not Displayed

Sorry if this question has been asked before. I hope someone can intervene and help me understand why my form validation errors do not appear.

I am using Spring 3.0.3 and Hibernate, and I am using jsr-303 validation to validate the input of my form. I have a Spring controller that processes a GETting page containing a form created using the Spring taglib form. In this form, the user can change his name and save it in the database. If any input is empty, the page with the form should again display with error messages. The same controller handles the page view. It seems that the controller is working fine in many ways, but when there is an error in the error filed by the user, no errors appear on the page.

Here's what the form looks like:

<form:form commandName="changeNameCommand">
    <form:errors path="*" cssClass="errorBox" />

    <table width="100%" border="0" cellspacing="5" cellpadding="5" align="left">
      <tr>
    <td><b>First Name:</b></td>
    <td><form:input path="firstName" value="${user.firstName}" /></td>
  </tr>
  <tr>
    <td><b>Last Name:</b></td>
    <td> <form:input path="lastName" value="${user.lastName}" /></td>
  </tr>
</table>
</form:form>    

Note that the view has a user object that is used to fill out the form with the current user name and user name. This is working correctly.

The controller looks something like this:

@Controller
@RequestMapping(value =  "/account/settings/change-name")
@SessionAttributes("changeNameCommand")
public class ChangeNameController {

    @ModelAttribute("changeNameCommand")
    public ChangeNameCommand getCommand() {
        return new ChangeNameCommand();
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getChangeNamePage(HttpServletRequest req) {
    ModelAndView mav = new ModelAndView("Account.ChangeName");
        mav.addObject("page_title", "Change Name");

        return mav;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String doChangeName(
                @ModelAttribute("changeNameCommand") 
                    @Valid ChangeNameCommand command, 
                    BindingResult result, SessionStatus status) {

        if (result.hasErrors()) {
            return "redirect:/account/settings/change-name"; 
        }

    // Code here to persist updated user first and last name to database...        

        status.setComplete();

        return "redirect:/home";
    }
}

I use Tiles 2.1.2 for page layouts and Urlrewrite 3.1.0 to help form friendly URLs.

The ChangeNameCommand class looks something like this:

import org.hibernate.validator.constraints.NotEmpty;

public class ChangeNameCommand {

    @NotEmpty(message = "You must provide a first name.")
    private String firstName;

    @NotEmpty(message = "You must provide a last name.")
    private String lastName;

    @NotEmpty(message = "Your password is required to make changes.")
    private String currentPassword;

    // Getters and setters here...
}

, , , BindingResult . . , redirect:. , ( ) - , Urlrewrite Spring .

/account/settings/change-name
/web/account/settings/change-name
/mywebsite/web/account/settings/change-name

. FWIW, -:

<servlet-mapping>
    <servlet-name>mywebsite</servlet-name>
    <url-pattern>/web/*</url-pattern>
</servlet-mapping>

!

+3
2

. UrlRewriteFilter Tiles. , , RequestMapping. , , doChangeName().

@Controller
@RequestMapping(value =  "/account/settings/change-name")
@SessionAttributes("changeNameCommand")
public class ChangeNameController {

    @ModelAttribute("changeNameCommand")
    public ChangeNameCommand getCommand() {        
        return new ChangeNameCommand();
    } 

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getChangeNamePage() {
        ModelAndView mav = new ModelAndView("Account.ChangeName");
        mav.addObject("page_title", "Change Name");

        return mav;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView doChangeName(@ModelAttribute("changeNameCommand") @Valid ChangeNameCommand command, 
            BindingResult result, SessionStatus status) {

        if (result.hasErrors
            ModelAndView mav = new ModelAndView("Account.ChangeName");
            mav.addObject("page_title", "Change Name");

            return mav;
         }

        // Code here to persist updated user first and last name to database...         

        status.setComplete();

        RedirectView view = new RedirectView("/home");
        return new ModelAndView(view);        
    }
}

, !

+1

redirect, ( ) . , . , - - ( - )

- UrlRewriteFilter spring -mvc . URL- REST spring -mvc.

+3

Source: https://habr.com/ru/post/1767702/


All Articles