0

Sorry for my dumb question and bad English. i trying to make some validation on user change password menu in my new Developed Web using Spring MVC.

i follow instruction from How to perform Spring validation in MultiActionController?

i write this code :

public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
    BindException bindException = (BindException) bindingException.getRootCause();
    ModelMap modelmap = new ModelMap();
    ChangePasswordForm cpForm = new ChangePasswordForm();
    cpForm.setUsername(SessionHelper.getUsername());
    modelmap.addAttribute("ChangePasswordForm", cpForm);
    return new ModelAndView("changepassword").addAllObjects(bindException.getModel()).addObject("ChangePasswordForm", cpForm);
}     

and in my jsp :

<form:form action="update.htm" commandName="ChangePasswordForm" >
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table  class="form">
        <tr>
            <td class="col1"><label>User Name :</label></td>
            <td class="col2"><form:label path="username" >${ChangePasswordForm.username}</form:label></td>
        </tr>
        <tr>
            <td><label>Old Password :</label></td>
            <td><form:password path="oldPassword" id="oldPassword"></form:password>
                <form:errors path="oldPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>New Password :</label></td>
            <td><form:password path="newPassword"></form:password>
            <form:errors path="newPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Confirm New Password :</label></td>
            <td><form:password path="confirmNewPassword"></form:password>
            <form:errors path="confirmNewPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="submit" ></td>
        </tr>
    </table>
</form:form>    

but it does not appear any error on my screen when i try negative test like empty field. when i try to print out

System.out.println("UserController::hanldeBindException::1" + bindException.getObjectName());

it show the object name is "command" instead of my command name in jsp which is "ChangePasswordForm". When i try to change that commandName to "command" it works. But i just curious is there some way to setObjectName in BindException Class or you have another way to use custom name instead of default "command" Object Name??

UPDATE

My Validation works since i change commandName to "command" to like this :

<form:form action="update.htm" commandName="command">
   <!-- Form Field and Error define here -->
</form:form>

and of course do some adjustment in my controller also. if i change it to another name it wont work anymore.

I use Spring Framework 3.0.2 RELEASE.

4

1 回答 1

0

simple answer would be that most likely you're extending BaseCommandController in some form (AbstractCommandController or otherwise), in which case you should probably override the "getCommandName()" and "getCommand()" methods. But i can't tell from the code you've supplied.

what version of spring are you using? The idea of a "command" is relatively antiquated now as of 3.0+ as are the base classes. Instead use annotation based controllers and model attributes:

@Controller
@SessionAttributes({"changePasswordForm"})
public class MyController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
       binder.setValidator(new ChangePasswordFormValidator());
    }

    @ModelAttribute("changePasswordForm")
    public ChangePasswordForm createChangePasswordForm() {
      ...
    }


    @RequestMapping(...)
    public ModelAndView controllerMethod(
          @Valid @ModelAttribute changePasswordForm ChangePasswordForm) {
      ...
    }
}

This should automatically validate your form before calling the controller method, and bounce back to the calling page.

In your validator, you'll supply error message code in the validate method:

public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "username.required");
}

You would then make sure "username.required" is a key in whatever MessageSource you've supplied to spring.

The form tags should work exactly as you have them.

Here's the spring reference manual for controllers:

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-controller

于 2013-02-01T05:36:37.877 回答