2

Ok. I'm making a java web app with a database backend to do some CRUD on some data. When the edit button is clicked next to an item, it navigates to a form with the current data for editing. One of the fields is boolean and I would like to display it as a checkbox so that True makes it checked and False leaves it unchecked.

I have tried many different variations none seem to work. Here are some examples where <%= action.get("stable")%> returns a string with either True or False

<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>

<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>

<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>

So how do you set a check box to checked/unchecked depending on the string returned with action.get("stable")

Thank you for any help sorry if the question is a bit trivial.

Vaandu
  • 4,857
  • 12
  • 49
  • 75
Zac
  • 121
  • 2
  • 3
  • 10

3 Answers3

5

I used this, and it worked perfectly.

 <input type="checkbox" <c:if test="${item.estado==2}">checked=checked</c:if> class="switch-input" >
3

The correct markup for a checked checkbox is checked="checked". If it's not checked, the checked attribute must not be present at all.

You should generate it using the JSTL and the JSP EL, because scriptlets are something from the past which should not be used in JSPs for years. See How to avoid Java code in JSP files?.

This would of course need some refactoring so that the action bean has a regular isStable() method returning a boolean, which would be much cleaner. But anyway, here's how it would work using your existing code :

<input type="checkbox" name="stable" <% 
    if ("True".equals(action.get("stable"))) {
        out.print("checked=\"checked\"");
    } %>/>

Note that all attributes should also be surrounded by quotes.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thank you i agree i'm not to sure why we are required to do scriptlets when it is not used any more. Anyway i appreciate your help. thank you – Zac Oct 21 '11 at 06:42
  • Why would you be required to do scriptlets? Is it homework where the teacher forces you to use scriptlets? If so, then kindly tell your teacher that he should change his lessons. If not, then don't use them. Use the EL, the JSTL and custom tags. – JB Nizet Oct 21 '11 at 06:45
  • yes im working on a project for tafe. This is the first web based ap we got so i think maybe the next one will be more up to date. I will ask him Monday tho =] – Zac Oct 21 '11 at 06:50
  • 3
    With JSTL: `checked=checked` assuming there is a request parameters with the key 'stable'. – hisdrewness Oct 21 '11 at 07:00
  • 1
    @hisdrewness: this would need the stable request param to be a boolean. And it would be better written as `checked="checked"` – JB Nizet Oct 21 '11 at 07:05
1

You need to set checked attribute of <input type="checkbox"/>

Edit:

<input type="checkbox" <%=action.get("stable") ? "checked='checked'" : "" %> />
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186