Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring MVC : form : radiobutton for Boolean property

I just want to know how to use a Boolean in a Spring mvc form.

I try with this code:

My jsp:

<form:radiobutton path="dateInterval" value="false" cssClass="radio"/>
<form:radiobutton path="dateInterval" value="true" cssClass="radio"/>

The property on the pojo:

private Boolean dateInterval = false;

But my dateInterval property is always null!

like image 848
BasicCoder Avatar asked Mar 28 '11 15:03

BasicCoder


People also ask

What is radio button in Spring MVC form?

The Spring MVC form radio button allows us to choose only one option at a time. This tag renders an HTML input tag of type radio. Apart from radio button tag, Spring MVC form tag library also contains radiobuttons tag.

How to set radiobutton properties in JSP form?

There is a JSP (user.jsp) that has two sets of radiobutton tag with options for user to select. The values for the properties are taken from an object of type UserPreferences bean which is bound using the attribute “modelAttribute” with in the form tag. The object is set with in the handler method of the Controller class.

What is a radio input in spring form?

It renders an HTML ‘input’ tag with type ‘radio’. The ‘radiobuttons’ is one of the tag provided by the spring-form.tld library. It renders multiple HTML ‘input’ tags with type ‘radio’. It will involve multiple tag instances bound to the same property but with different values by passing in the available options as a runtime variable.

How to render the HTML radiobuttons in HTML5 form?

Here, we are using <form:radiobuttons /> tag to render the HTML radiobuttons. For example − It will render the following HTML content.


2 Answers

I have them working on my form like this:

<form:radiobutton path="someProperty" value="true"/>
    <spring:message code="label.roundYes"/>
<form:radiobutton path="someProperty" value="false"/>
    <spring:message code="label.roundNo"/>

and in my model object the someProperty looks like this:

private boolean someProperty = false;

That works fine. I haven't tried it with 'Boolean'. Maybe just try it with boolean and see if it helps.

like image 88
blong824 Avatar answered Oct 19 '22 12:10

blong824


Just to clarify things: In my opinion it also works with Boolean Object. I have a form using Spring 3 and this setup is running perfect (using true/false/null Values as an Option):

Form JSP:

<form:radiobutton path="tour.routeNachZeit" value="true" />
<form:radiobutton path="tour.routeNachZeit" value="false" />

Model Object (named Tour):

private Boolean routeNachZeit;

Therefore I don't unterstand why I should have changed my Property to simple boolean. This works in my case.

My help came from this post in the Spring Forum.

like image 33
leole Avatar answered Oct 19 '22 11:10

leole