在Spring MVC中从数据库中获取复选框值 [英] Fetching checkbox value from database in Spring MVC
问题描述
我试图根据存储在数据库中的值在JSP中选中/取消选中我的复选框字段.jsp的代码段为:
I am trying to check/uncheck my checkbox field in JSP,based on the values stored in my db.The code snippet for jsp is:
<div class="form-group t-margin-top-10">
<label for="defaultContact" class="col-md-5 col-lg-4 control-label"></label>
<div class="col-lg-4 ">
<input type="checkbox" id="defaultContact${billingContactDto.billingContactId}" name="defaultContact" /> Default Contact</div>
这没有显示默认检查值(在这种情况下为选中). billingContactDto是联系人对象, billingContactId是billingContact对象中的PK. billingContact.defaultContact是billingContact中的字符串(此处未显示)
This is not showing default checked value (checked in this case). billingContactDto is contacts object, billingContactId is PK in billingContact object. billingContact.defaultContact is a string in billingContact(not shown here)
问题是如何从db获取复选框的值. 预先感谢
The question is how to fetch values of checkbox from db. Thanks in advance
推荐答案
我试图根据存储在数据库中的值来选中/取消选中JSP中的复选框字段
I am trying to check/uncheck my checkbox field in JSP,based on the values stored in my db
尝试使用 JSTL & EL 而不是Scriplet元素.对于数据库访问,请使用JSTL SQL标记库.
Try to use JSTL & EL instead of Scriplet elements. For database access use JSTL SQL Tag library.
最好在Servlet中移动数据库代码.
It's better to move the database code in the Servlet.
要遵循的步骤:
- 只需从数据库中获取记录
- 将结果存储在对象或列表中
- 在请求属性中设置
- 重定向到JSP
- 使用JSTL或EL从JSP中的request属性访问值
尝试使用 HTML选中的属性
示例代码:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="gender" value="female"/>
Male <input type="checkbox" name="R1"
value="Male" <c:if test="${gender=='male'}">checked</c:if>>
Female <input type="checkbox" name="R1"
value="Female" <c:if test="${gender=='female'}">checked</c:if>>
看看类似的 查看全文