Jsp 有没有更好的方法来生成单选按钮?
根据Jsp 有没有更好的方法来生成单选按钮?,jsp,servlets,jstl,el,Jsp,Servlets,Jstl,El,根据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 >Hob
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>
霍比1
霍比2
霍比3
有没有更好的方法来生成单选按钮,因为现在我们正在为每个按钮编写静态代码。我们正在检查每个按钮?是的,您不需要JSTL。艾尔就够了 试试这个
<input type="radio" value="hobby3" name="hobby" ${(hobby == 'hobby3')?'checked':''} />
正在使用${(hobby=='hobby3')
是正确的,或者我们应该使用${(hobby.equals(“hobby3”))
?如果jstl中的运算符==
是具有相同值的不同对象,则表示它们返回true?您可以同时执行这两项操作。EL使用值而不是对象引用。此外,旧容器中不支持方法调用。您也可以使用eq
。