Question

I would like to use form:radiobuttons tag from spring form tag lib to get input form user , but I don't want to use model/command object since this is the only value I get as a input.So I tried below.

JSP:

<form action="submitform" method="post" >
<form:radiobuttons path="lang"  items="${languages}"/>
<input type="submit" value="Save"/>
</form>

Controller's method:

@RequestMapping("submitform")
public ModelAndView submitForm(@RequestParam("lang") List<String> lang) {
    System.out.println("test:"+lang);
    return new ModelAndView("test", "lang", lang);
}

When I submit the form, application throws below error. HTTP Status 400 - Required List parameter 'lang' is not present.

Can anyone tell me the correct way to do this,I have two below requirements

1.Want to use form:radiobuttons.

2.Don't want to use model/command object.

Please help

Was it helpful?

Solution

How to use form:radiobuttons with out model/command object

you cannot use spring's form elements without having a form backing bean. the path attribute of spring's form element tag is supposed to specify the path to the model/command bean's property for data binding.

Spring javadoc have about <form:radiobuttons is:

| Attribute  | Required? | Runtime Expression? | Description
------------------------------------------------------------
|    path    |  true     |     true            |Path to property for data binding

that means, in your case when you don't have model/command bean for data binding, then spring will throw a exception with message as:

Neither BindingResult nor plain target object for bean name 'command' available

Can anyone tell me the correct way to do this

use plain HTML radiobuttons in jsp and keep as it all your methods. in jsp render radiobuttons like:

<form action="submitform" method="post" >
    <%-- <form:radiobuttons path="lang"  items="${languages}"/> --%>
    <c:forEach items="${languages}" var="lng">
        <input type="radio"
                name="lang"
                value="${lng}">${lng}
    </c:forEach>
<input type="submit" value="Save"/>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top