【问题标题】:How to populate a hidden HTML table with the result from the database?如何使用数据库中的结果填充隐藏的 HTML 表?
【发布时间】:2014-05-03 09:33:05
【问题描述】:

我有一个名为create-account.jsp 的JSP,它收集客户的详细信息并将数据发送到Servlet。 Servlet 将客户的数据插入到数据库中,查询数据库并将结果设置为请求范围的属性,然后分派到create-account.jsp(same jsp)。现在插入的详细信息必须出现在 jsp 中的隐藏 html 表中。我该怎么做?有人可以帮忙吗?

create-account.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Create New Account</title>
    <style>
        a {
            text-decoration: none;
        }

        body {
            border-color: azure;
        }

    </style>
</head>
<body>
<p align="right">Hi, ${sessionScope.user.userName} <a
        href="${pageContext.servletContext.contextPath}/logOut.do">logout</a></p>


<h2 align="center">Create New Account</h2>

<form action="${pageContext.request.contextPath}/create.do" method="post">
    <table border="1" align="center">
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>DOB:(yyyy-mm-dd)</td>
            <td><input type="text" name="dob"></td>
        </tr>
        <tr>
            <td>Balance:</td>
            <td><input type="text" name="balance"></td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <input type="submit" value="Create" name="create">
            </td>
        </tr>
    </table>
</form>
<p align="center" style="display: none">${requestScope.result} has been successfully inserted into the table</p>
<table id="hiddenTable" style="display: none" border="1">
    <tr>
        <td>

        </td>
    </tr>
</table>
</body>
</html>

【问题讨论】:

    标签: java html jsp servlets


    【解决方案1】:

    使用

    style="visibility: hidden;"
    

    style="display:none;"
    

    隐藏表格。

    欲了解更多信息,请查看以下链接


    如何在表格中显示结果?

    示例代码:

    小服务程序:

    public class Model{
         private String name;
         private String dob;
         private double balance;
         // getter and setter   
    }
    
    ...
    
    List<Model> list = new ArrayList<Model>();
    //add the data in list
    
    request.setAttribute("list",list);
    

    JSP:

    <c:if test="${not empty list}">
      <c:forEach var="ob" items="${list}">
         <tr>
           <td><c:out value="${ob.name}"/></td>
           <td><c:out value="${ob.dob}"/></td>
           <td><c:out value="${ob.balance}"/></td>
        </tr>
      </c:forEach>
    </c:if>
    

    完整代码请查看jsp iterate over list

    【讨论】:

    • 以后如何取消隐藏?我应该使用一个布尔属性吗?我该怎么做?
    • 谢谢,我也在尝试同样的事情。
    • 我的 JavaScript 有点土。感谢您的链接。
    猜你喜欢
    • 2021-04-26
    • 2017-03-18
    • 1970-01-01
    • 2017-11-06
    • 2011-12-22
    • 1970-01-01
    • 2020-03-26
    • 2021-01-10
    • 2015-01-21
    相关资源
    最近更新 更多