Results 1 to 5 of 5

Thread: Data binding without using spring taglib

  1. #1
    Join Date
    Jan 2011
    Posts
    8

    Default Data binding without using spring taglib

    Hi,

    My html is built without using the spring taglib and now I'd like to bind the parameters of the form to a object in my controller.

    Currently my form looks like this

    Code:
    <form>
    <input type="text" name="frAccUserMgmt.userName"/>
    <input type="password" name="frAccUserMgmt.userPwd"/>
    </form>
    The relevant part of my object is

    Code:
    Class FrAccUserMgmt {
        private String userName;
        private Strint userPwd;
        // getter and setter
    }
    My controller is

    Code:
    @RequestMapping("login")
    Public ModelAndView doLogin(FrAccUserMgmt frAccUserMgmt) {
        //code
    }
    Adding a BindingResult parameter to the controller doesn't help. It just simply says that there are no binding errors. Neither does adding a @ModelAttribute help.
    How do I go about binding it? I just get an empty object in my code.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Why not use the binding tags? Makes your life a lot easier but alas. Remove the frAccUserMgmt part of the name and make sure you have a get/setUserName etc. But beware it is oneway only! If there are errors you will not find your input fields pre-filled with the old values but get an empty form again...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2011
    Posts
    8

    Default

    Man you are awesome! I wouldn't have thought that spring would've been able to bind the parameters to the object without a reference to the name of the object like "frAccUserMgmt". This also means that I can't have two objects with the same field say addressLine1 as method parameters in the controller since spring won't be able tell which object to bind the addressLine1 field to, right? Or is there a way in which I can specify a form field to object field mapping?

    I'm not using the spring taglib since I'm not the one who's developing the front end. I suck at html. A professional HTML designer is making the front end who doesn't know Java. Plus he's using ExtJs which generates HTML markup using JavaScript.

    Thanks for the help!
    Varun

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    You're welcome. If you aren't in control of the front-end you might want to take a look at Thymeleaf with that you simply reuse the HTML, add some additional parameters to the HTML tag and you get the best of both worlds.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jan 2011
    Posts
    8

    Default

    Thanks for the suggestion. I'll look it up.

    Thanks again,
    Varun

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •