我的目标是从数据库中检索值并在JSP中显示。
单选按钮
如果数据库数据是所有者,将检查所有者单选按钮。如果数据库数据是收银员,则将检查收银员。
下拉列表
如果数据库数据为橙色,则将选择橙色选项。
以下是我的代码。
帮助将不胜感激。谢谢! :)
单选按钮
<input type="radio" name="role" id="Owner" value="Owner" <c:if out='${staff.staffRole} == "Owner"'>checked</c:if>/>
<input type="radio" name="role" id="Cashier" value="Cashier" <c:if out='${staff.staffRole} == "Cashier"'>checked</c:if>/>
下拉列表
<select class="form-control">
<option>Apple</option>
<option>Orange</option>
<option>Durian</option>
</select>
答案 0 :(得分:3)
对于Radio Buttons:
<c:choose>
<c:when test='${staff.staffRole == "Owner"}'>
<input type="radio" name="role" id="Owner" value="Owner" checked >
</c:when>
<c:otherwise>
<input type="radio" name="role" id="Owner" value="Owner">
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${staff.Cashier} == "Owner"}'>
<input type="radio" name="role" id="Cashier" value="Cashier" checked >
</c:when>
<c:otherwise>
<input type="radio" name="role" id="Cashier" value="Cashier" value="Owner">
</c:otherwise>
</c:choose>
对于DropDown 假设你的数据在staffFruit
下的bean中是相同的bean <select class="form-control">
<c:choose>
<c:when test='${staff.staffFruit == "Apple"}'>
<option selected>Apple</option>
</c:when>
<c:otherwise>
<option>Apple</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${staff.staffFruit == "Orange"}'>
<option selected>Orange</option>
</c:when>
<c:otherwise>
<option>Orange</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${staff.staffFruit == "Durian"}'>
<option selected>Durian</option>
</c:when>
<c:otherwise>
<option>Durian</option>
</c:otherwise>
</c:choose>
</select>
这是一个简单的if else梯形图。我建议你使用更方便的东西,比如
答案 1 :(得分:0)
你可以使用简单的jsp划线来做得更好。
单选按键:强>
<%
String ownerChecked = "";
String cashierChecked = "";
if(staff.staffRole.equals("Owner")){
ownerChecked = "checked";
}else{
cashierChecked = "checked";
}
%>
<input type="radio" name="role" id="Owner" value="Owner" <%=ownerChecked %> />
<input type="radio" name="role" id="Cashier" value="Cashier" <%=cashierChecked %> />
下拉列表:
<select class="form-control">
<option selected="<%=staff.staffFruit.equals("Apple") %>">Apple</option>
<option selected="<%=staff.staffFruit.equals("Orange") %>">Orange</option>
<option selected="<%=staff.staffFruit.equals("Durian") %>">Durian</option>
</select>
试试这个并通知我是否需要进一步的协助。
答案 2 :(得分:0)
更改
${staff.staffRole} == "Owner"
要
${staff.staffRole == "Owner"}
答案 3 :(得分:0)
您可以使用JSTL eq
operator
<c:if out="${staff.staffRole eq 'Owner'}"> ......
或者
<c:if out="${staff.staffRole == 'Owner'}"> .....