Announcement Announcement Module
Collapse
No announcement yet.
how to bind data to a java class using Ajax and Controller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to bind data to a java class using Ajax and Controller?

    class:
    Code:
    public class Person implements Serializable {
        private int id;
        private String name;
        private List<Address> addresses;
    }
    controller:
    Code:
    public class PersonController extends MultiActionController {
        private RegistrationService registrationService;
        
        @RequestMapping(method = RequestMethod.POST)
        public void register(HttpServletRequest req, HttpServletResponse res) {
            Person person = bindPostedData(req);
            
            registrationService.register(Person);
            .......
        }
    
        private Person bindPostedData(HttpServletRequest req) {
            Person person = new Person();
            person.setId(req.getParamter("id"));
            person.setName(req.getParamter("name"));
            ......
    
            return person;
        }
    }
    upon ajax call, I serialize the form elements one by one.

    example JS (register button click event):
    Code:
    var person = {
        "id": $("#person\\.name").val(),
        "name": $("#person\\.name").val(),
        .....
    }
    
    ajaxcall(person);
    before that, it was a spring webflow (swf 2.0.9.RELEASE, mvc 2.5.6.SEC02). Instead of ajaxcall(), I use form submit then trigger the webflow event. Directly call the registrationService.register(flowScope.Person) and the bind was automated.

    In some reason, I need to use Ajax and call the service via controller. But... I had problem with data binding that's why I get the data from request object (posted data) then bind to Person class which is very tedious.

    Please guide me or give me some hints to investigate.

    Thanks in advanced.
    Last edited by eros; Apr 22nd, 2011, 02:14 AM.

  • #2
    anyone here doing the same approach?
    please guide me on.

    Comment


    • #3
      First of all, you are mixing new annotated controller strategy with old, deprecated controllers. This is bad, remove the "extends MultiActionController" and only use annotated @Controllers with <mvc:annotation-driven/>.

      Once you got this done, and Spring mvc is properly configured with mvc:annotation-driven, I suggest you take a look at the DWR framework which integrates perfectly with Spring 3 and relieves you of the burden of JSON serialization-deserialization. In short, it lets you use Java objects directly in javascript: ajax call, json serialization, and all the "boring" stuff is handled automatically by the framework. I have this useful link that teaches how to integrate DWR with Spring so that you can use Spring annotations to mark DWR objects:

      http://www.codercorp.com/blog/spring...notations.html

      Comment


      • #4
        Thanks for the response.

        If you scroll down the comments. You may notice that I already commented on November 30th, 2009. For performance issue, I opted no for DWR.

        Comment

        Working...
        X