zond
zond

Reputation: 1480

JSP set boolean value in form

I have a Model:

public class Header {
    private Boolean SERVICE;
}

Controller:

@RequestMapping("mymodel/Edit")
public ModelAndView mymodelEdit(
        @ModelAttribute("mymodel") Mymodel mymodel,
        @RequestParam String id) {      

    Mymodel old_mymodel = mymodelService.getMymodel(id);
    Map<String, Object> map = new HashMap<String, Object>();            
    map.put("old_mymodel", old_mymodel);
    return new ModelAndView("mymodel/mymodelEditView", "map", map);
}

JSP Form

<c:set var="old_mymodel" value="${map.old_mymodel}" />
<form:form method="POST action="/mymodel/Save" modelAttribute="mymodel">
    <tr>
        <td>Сервис :</td>
        <td>
            <form:checkbox path="SERVICE" value="${old_mymodel.SERVICE}">                 
            </form:checkbox>
        </td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="Save" /></td>
    </tr>
    </table>
</form:form>

My problem: I can't set a value from db to form value, i.e. when SERVICE value is true, checkbox is not checked.

Upvotes: 0

Views: 5904

Answers (2)

Karibasappa G C
Karibasappa G C

Reputation: 2732

first thing you are setting the value of map to variable like below

<c:set var="old_header" value="${map.old_mymodel}" />

so you have to access the SERVICE boolean value using this variable not map.

so it should be accessed like below

<td><form:checkbox path="SERVICE" value="${old_header.SERVICE}"></form:checkbox></td>

instead of

<td><form:checkbox path="SERVICE" value="${old_mymodel.SERVICE}"></form:checkbox></td>

where you are using old_mymodel,

assuming below code returns correct model

Mymodel old_mymodel = mymodelService.getMymodel(id);

Upvotes: 0

geoand
geoand

Reputation: 64011

The way you are trying to access the model does not correspond to the way you have populated it.

I propose you change your code to:

@RequestMapping("mymodel/Edit")
public ModelAndView mymodelEdit(
        @ModelAttribute("mymodel") Mymodel mymodel,
        @RequestParam String id) {      

    Mymodel old_mymodel = mymodelService.getMymodel(id);
    return new ModelAndView("mymodel/mymodelEditView", "model", old_mymodel);
}

and

That is assuming that Mymodel looks something like:

public class Mymodel {
   private Header old_header;
}

Also there might be some problems with the names you have used in various parts of the model. I strongly suggest that you adhere to JavaBean naming conventions

Upvotes: 1

Related Questions