开发者

JSP html form generation based on JDBC meta data

开发者 https://www.devze.com 2023-02-28 00:30 出处:网络
could anybody give me a good idea or hint (not a tool) how I could implement a JDBC meta data based automatic generation of html forms?

could anybody give me a good idea or hint (not a tool) how I could implement a JDBC meta data based automatic generation of html forms? I have solved this before in a Java standalone progra开发者_高级运维m - now I want to expand the idea within html. I do NOT want to use Spring, Wicket or JSF to solve the problem, I want to do it myown to learn from scratch. I have searched really a lot, I found a lot of stuff, but nothing which could answer my question. But I am sure this problem has been solved in the past a dozen times.

Thank you Alex


Create a servlet class, let it call your "Java standalone program" in the doGet() method, get the desired data in form of some List<Row> from it, put it in the request scope and forward the request to JSP

List<Row> rows = yourProgram.list();
request.setAttribute("rows", rows);
request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response);

And finally in /WEB-INF/list.jsp use the JSTL c:forEach to display it.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<table>
    <tr>
        <c:forEach items="${rows[0].columns}" var="column">
            <th><c:out value="${column.name}" /></th>
        </c:forEach>
    </tr>
    <c:forEach items="${rows}" var="row">
        <tr>
            <c:forEach items="${row.columns}" var="column">
                <td><input type="text" name="${fn:escapeXml(column.name)}" value="${fn:escapeXml(column.value)}" /></td>
            </c:ForEach>
        </tr>
    </c:forEach>
</table>

Let it submit to a servlet by <form method="post"> and gather the data in doPost() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消