user990855
user990855

Reputation: 67

Setting check boxes on JSP

I am having trouble setting the default values of my checkboxes on my JSP page. I have a form and a command linked to it. The first time the page displays the command has all the values for the checkboxes as false (the attributes are boolean). Then when you check them, the command's attributes should change to true. I add this command to the session and on a back button from a previous page, the boxes that was checked should be again. Can someone see something wrong? (The input boxes work and keep their data)

<form class="form-horizontal" commandName="goalDetailCommand" name="formdetail" id="formdetail" method="post">

<input type="checkbox" path="asMuchAsPossible" name="asMuchAsPossible" id="asMuchAsPossible"
                                                   value="${goalDetailCommand.asMuchAsPossible}">

The code is a simple object:

public class GoalDetailCommand {

private boolean asMuchAsPossible;

public boolean isAsMuchAsPossible() {
    return asMuchAsPossible;
}

public void setAsMuchAsPossible(boolean asMuchAsPossible) {
    this.asMuchAsPossible = asMuchAsPossible;
}

Upvotes: 0

Views: 250

Answers (1)

Rob Garwood
Rob Garwood

Reputation: 108

<input type="checkbox" path="asMuchAsPossible" name="asMuchAsPossible" id="asMuchAsPossible" <c:if test="${goalDetailCommand.asMuchAsPossible}">checked</c:if>/>

Would work, although it doesn't seem very elegant...

Upvotes: 1

Related Questions