Announcement Announcement Module
Collapse
No announcement yet.
Json Binding and spring Form Tags Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Json Binding and spring Form Tags

    Hello all, first time here. I'm a .NET guy in the process of converting to Java so bear with me if my questions reflect my inexperience.

    We're writing a Spring MVC application (3.1) and I've run into a bit of a problem binding a model via json. The client submits json to the controller action and my method signature looks like so:

    Code:
    public ModelAndView editCampaign(@RequestBody @Valid Campaign campaign, HttpServletRequest request){
    ...
    }
    The reason we're returning ModelAndView is that our convention has been to rewrite the form, via ajax. The forms are built using spring form tags.

    When there is a problem with validation we have this generic error handler in our controller:

    Code:
    @ExceptionHandler(MethodArgumentNotValidException.class)
    	public ModelAndView handleValidationException(MethodArgumentNotValidException  exception, HttpServletRequest request) { ... }
    We're using JSR-303 annotations for field level validations.

    Inside the previous function we process the form, and it seems as though the information is bound correctly but the error tags are not. Before we were doing model binding via form submits, we did not need an exception handling method and everything worked fine.

    I can't find the disconnect and why the error tags are not getting processed correctly based on the validation exceptions.

    Thanks in advance for your time.

    -jbenckert

  • #2
    Hi,
    I am in the developing of a Spring database application for user registration and login authentication. After configuring beans in dispatcher-servlet properly, I used Spring bind tag and form tags in that
    In my jsp I used

    <form:form>
    <Spring:bind path="loginUser.username">
    <input type="text" name="<c ut value='${status.expression}'/>"
    value="<c ut value='${status.value}'/>" size="20"/>
    </Spring:bind>

    ..........
    .........

    </form:form>

    I have the bean configured in the xml file with the id "loginUser" with two properties with the names "username" and "password" and they have values set.

    When I try to bind the form inputs to command object the following error comes.

    Neither BindingResult nor plain target object for bean name 'loginUser' available as request attribute.

    Also I used the

    setCommandName(LoginUser.class);

    setCommandClass("loginUser");

    methods inside the LoginController's constructor.

    Could anybody explain how to bind form inputs to command object and retrieve it.

    Thanks

    Ramesh Kangamuthu

    Comment


    • #3
      thank you jbenckert you share such a great and use full reply, it will solve my prob also, appreciated

      Comment

      Working...
      X