Search code examples
javagwtgwt2

Retrieve GWT radiobutton value in servlet


I'm having a headache figuring how to retrieve the gwt Radio Buttons values in the server side.

Here is my UiBinder form:

<g:FormPanel ui:field="form"><g:VerticalPanel ui:field="fruitPanel">
    <g:RadioButton name="fruit">apple</g:RadioButton>
    <g:RadioButton name="fruit">banana</g:RadioButton>
    <g:SubmitButton>Submit</g:SubmitButton> ...

Here is how i initialize the form:

form.setAction("/submit");
form.setMethod(FormPanel.METHOD_POST);

So i though i would have to do this on the servlet:

fruit = req.getParameter("fruit")

But of course this doesn't work, parameter fruit doesn't exist :/

Edit: Ok i get parameter fruit but it's always "on"

I also did try to add the radio button in java with:

RadioButton rb0 = new RadioButton("fruit", "apple");
RadioButton rb1 = new RadioButton("fruit", "banana");
fruitPanel.add(rb0);
fruitPanel.add(rb1);

Edit: This is a GWT issue: Issue 4795


Solution

  • since I cannot comment on the question: Which version of GWT are you using?

    I've create the exact same template as you did and Firebug tells me that it's posting:

    "fruit=on"

    Of course this payload is only posted when one of the checkboxes is checked. ;-)

    But beware: I've recognized recently that GWT doesn't set the "value" of the radio button when used inside UiBinder template and instead just sends "on" as value which makes the radio button more or less useless to be used in a UiBinder template.

    HTH Max