根据hobby
中的值生成动态单选按钮。我的工作是 -
request.setAttribute("hobby", null);
//values of hobby can be "hobby1" or "hobby2" or "hobby3" or null
request.getRequestDispatcher("display.jsp").forward(request, response);
在display.jsp中我正在生成单选按钮 -
<tr>
<td >Hobby1
<c:choose>
<c:when test='${hobby.equals("hobby1")}'>
<input type="radio" value="hobby1" name="hobby" checked/>
</c:when>
<c:otherwise>
<input type="radio" value="hobby1" name="hobby"/>
</c:otherwise>
</c:choose>
</td>
<td >Hobby2
<c:choose>
<c:when test='${hobby.equals("hobby2")}'>
<input type="radio" value="hobby2" name="hobby" checked/>
</c:when>
<c:otherwise>
<input type="radio" value="hobby2" name="hobby"/>
</c:otherwise>
</c:choose>
</td>
<td >Hobby3
<c:choose>
<c:when test='${hobby.equals("hobby3")}'>
<input type="radio" value="hobby3" name="hobby" checked/>
</c:when>
<c:otherwise>
<input type="radio" value="hobby3" name="hobby"/>
</c:otherwise>
</c:choose>
</td>
</tr>
有没有更好的方法来生成单选按钮,因为现在我们正在为每个按钮编写静态代码。我们正在检查每个按钮?
答案 0 :(得分:0)
是的,您不需要JSTL。 EL就足够了。
试试这个
<input type="radio" value="hobby3" name="hobby" ${(hobby == 'hobby3')?'checked':''} />