有没有更好的方法来生成单选按钮?

Is there any better approach to generate radio button?

根据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>

有没有更好的方法来生成单选按钮,因为现在我们正在为每个按钮编写静态代码button.We正在检查每个按钮?

是的,您不需要 JSTL。 EL就够了。

试试这个

<input type="radio"  value="hobby3" name="hobby" ${(hobby == 'hobby3')?'checked':''} />