So I got this checkbox:
<form:checkbox id="someCheckbox" path="someCheckbox"/>
Now I would like to add an attribute - specifically checked="checked"
if a condition is true. Is this possible without wrapping the entire element in an if statement?
Basically what I want (results in an error):
<form:checkbox id="someCheckbox" path="someCheckbox" ${isChecked ? 'checked=checked' : ''}/>
Note that adding the attribute checked=""
, and making the value of it conditional will not work either, since checked=false or checked=' ' is treated as checked in modern browsers.
its better if you put the logic in controller
String checked=null;
if(condition)
checked="checked"
else
checked="";
modelMap.put("checkBoxChecked",checked);
return modelmap to view
and in view add as below
<form:checkbox id="someCheckbox" path="someCheckbox" ${checkBoxChecked}/>
Include core jstl tag in your jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
and then
<input type="checkbox" name="someCheckbox" id="someCheckbox" <c:if test="${check your condition}">checked="checked"</c:if> />
This works for me in Tomcat 7/JSP 2.2/JSTL 1.2:
<form:checkbox id="someCheckbox" path="someCheckbox"
<c:if test="${isChecked}">
checked="checked"
</c:if>
/>
Using the ternary operator like in your question should work with JSP >= 2.0:
<form:checkbox id="someCheckbox" path="someCheckbox"
${isChecked ? "checked='checked'" : ""}
/>
Or you could use this:
<c:choose>
<c:when test="${isChecked}">
<c:set var="varChecked" value="checked='checked'"></c:set>
</c:when>
<c:otherwise>
<c:set var="varChecked" value=""></c:set>
</c:otherwise>
</c:choose>
<form:checkbox id="someCheckbox" path="someCheckbox"
${varChecked}
/>
Still less verbose and redundant than wrapping the entire form-tag in a c:if-tag.
Obviously you'll need to include the core taglib for any of these to work.
Since form:checkbox is bind with form through path attribute, it's enough if you set the value in your form object.
Eventually do it with javascript
So I got this checkbox:
<form:checkbox id="someCheckbox" path="someCheckbox"/>
Now I would like to add an attribute - specifically checked="checked"
if a condition is true. Is this possible without wrapping the entire element in an if statement?
Basically what I want (results in an error):
<form:checkbox id="someCheckbox" path="someCheckbox" ${isChecked ? 'checked=checked' : ''}/>
Note that adding the attribute checked=""
, and making the value of it conditional will not work either, since checked=false or checked=' ' is treated as checked in modern browsers.
its better if you put the logic in controller
String checked=null;
if(condition)
checked="checked"
else
checked="";
modelMap.put("checkBoxChecked",checked);
return modelmap to view
and in view add as below
<form:checkbox id="someCheckbox" path="someCheckbox" ${checkBoxChecked}/>
Include core jstl tag in your jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
and then
<input type="checkbox" name="someCheckbox" id="someCheckbox" <c:if test="${check your condition}">checked="checked"</c:if> />
This works for me in Tomcat 7/JSP 2.2/JSTL 1.2:
<form:checkbox id="someCheckbox" path="someCheckbox"
<c:if test="${isChecked}">
checked="checked"
</c:if>
/>
Using the ternary operator like in your question should work with JSP >= 2.0:
<form:checkbox id="someCheckbox" path="someCheckbox"
${isChecked ? "checked='checked'" : ""}
/>
Or you could use this:
<c:choose>
<c:when test="${isChecked}">
<c:set var="varChecked" value="checked='checked'"></c:set>
</c:when>
<c:otherwise>
<c:set var="varChecked" value=""></c:set>
</c:otherwise>
</c:choose>
<form:checkbox id="someCheckbox" path="someCheckbox"
${varChecked}
/>
Still less verbose and redundant than wrapping the entire form-tag in a c:if-tag.
Obviously you'll need to include the core taglib for any of these to work.
Since form:checkbox is bind with form through path attribute, it's enough if you set the value in your form object.
Eventually do it with javascript
0 commentaires:
Enregistrer un commentaire