【问题标题】:Fetching checkbox value from database in Spring MVC在 Spring MVC 中从数据库中获取复选框值
【发布时间】:2014-07-17 03:49:21
【问题描述】:

我正在尝试根据存储在我的数据库中的值来检查/取消选中 JSP 中的复选框字段。jsp 的代码 sn-p 是:

<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 中的一个字符串(此处未显示)

问题是如何从数据库中获取复选框的值。 提前致谢

【问题讨论】:

  • 这篇文章涉及两件事。 1)根据存储在数据库中的值设置复选框的默认状态 2)如何从数据库中获取值。

标签: jsp


【解决方案1】:

我正在尝试根据存储在我的数据库中的值检查/取消选中 JSP 中的复选框字段

尝试使用 JSTLEL 代替 Scriplet 元素。对于数据库访问,请使用 JSTL SQL Tag library

最好在Servlet中移动数据库代码。

要遵循的步骤:

  • 只需从数据库中获取记录
  • 将结果存储在对象或列表中
  • 在请求属性中设置
  • 重定向到 JSP
  • 使用 JSTL 或 EL 访问 JSP 中请求属性的值

试试HTML checked Attribute

示例代码:

<%@ 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>>

看看类似的post

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    相关资源
    最近更新 更多