npinti
npinti

Reputation: 52185

Access GWT Form Values from Spring Controller

I have started learning GWT and I would like to know if it is possible to create a form in GWT for a process such as user registration and then, handle the logic of the registration(extra validation, adding the data to the database, etc) in a spring controller. I have so far been unable to find resources on the web to do this, so I am guessing that what I am after might not be possible. Can this only be done using classes which extend the RemoteServiceServlet as shown in this video tutorial?

I have tried to access the data from my spring controller using the request.getParameter() method calls, what I noticed was that when I used the Post method, I was unable to access the form parameters, but when I use Get, I can access them. This is some of the code I am using:

GWT:

        HorizontalPanel hrzPnlname = new HorizontalPanel();
        hrzPnlname.add(new Label("User Name: "));

        final TextBox txtUserName = new TextBox();
        txtUserName.setName("userName");        
        hrzPnlname.add(txtUserName);...

Spring:

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView helloWorldFromForm(HttpServletRequest request)
    {
        Map<String, Object> model = new HashMap<String, Object>();
        System.out.println("------>\n\n\n\n\n\n" + request.getParameter("userName") + "\n\n\n\n\n<-------");...

I am using GWT 2.4. Any information on this would be highly appreciated.

Thanks!

Upvotes: 0

Views: 1278

Answers (2)

Vladimir Korobkov
Vladimir Korobkov

Reputation: 619

You can share data between GWT client and spring controllers in a several ways:

  1. REST/JSON request
    See how: Calling REST from GWT with a little bit of JQuery
    See also original GWT documentation(section "Communicating with the server/JSON")
    This is the best way to communicate with a server in my opinion, because JSON is a pure standard protocol and it can be used by 3-rd party plugins(jQuery for example) and other web services.

  2. Async GWT RMI(Remote method invocation), provided by Google
    I think this is not the best idea to use GWT RPC, see why: 4 More GWT Antipatterns

  3. Submit forms directly to spring controller as POST/GET request (as you trying to do).
    See com.google.gwt.user.client.ui.FormPanel
    I can reconmmend you to use forms submitting only for file uploading, because the browser will only upload files using form submission. For all other operations, form is not required in GWT, any request possible to implement using ajax.

Upvotes: 2

Related Questions