Announcement Announcement Module
Collapse
No announcement yet.
Data binding without using spring taglib Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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...

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          Thanks for the suggestion. I'll look it up.

          Thanks again,
          Varun

          Comment

          Working...
          X