Results 1 to 4 of 4

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

Threaded View

  1. #1
    Join Date
    Oct 2008
    Posts
    286

    Default 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 at 01:14 AM.
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

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
  •