使用Java編寫表單資料的展示與瀏覽功能
概述:
在網頁開發中,表單是非常常見的一種資料輸入方式。在許多場景中,我們需要展示已提交的表單數據,以便用戶查看和瀏覽。本文將引導您使用Java來撰寫簡單的表單資料來展示與瀏覽功能。
實作步驟:
範例程式碼:
public class FormData { private int id; private String name; private int age; private String email; // 构造方法 public FormData(int id, String name, int age, String email) { this.id = id; this.name = name; this.age = age; this.email = email; } // setter和getter方法 public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } }
範例程式碼:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class FormDataDAO { private static final String DB_URL = "jdbc:mysql://localhost:3306/mydb"; private static final String DB_USER = "root"; private static final String DB_PASSWORD = "password"; public List<FormData> getAllFormData() { List<FormData> formDataList = new ArrayList<>(); try { // 连接数据库 Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); // 执行查询语句 String sql = "SELECT * FROM form_data"; PreparedStatement statement = conn.prepareStatement(sql); ResultSet rs = statement.executeQuery(); // 遍历结果集,将数据封装为FormData对象 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String email = rs.getString("email"); FormData formData = new FormData(id, name, age, email); formDataList.add(formData); } // 关闭数据库连接 rs.close(); statement.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return formDataList; } }
範例程式碼:
import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FormViewServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 调用数据访问层代码,获取表单数据 FormDataDAO formDataDAO = new FormDataDAO(); List<FormData> formDataList = formDataDAO.getAllFormData(); // 将表单数据传递给视图层进行展示 request.setAttribute("formDataList", formDataList); request.getRequestDispatcher("form_view.jsp").forward(request, response); } }
範例程式碼:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>表单数据展示</title> </head> <body> <table> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>邮箱</th> </tr> <c:forEach var="formData" items="${formDataList}"> <tr> <td>${formData.id}</td> <td>${formData.name}</td> <td>${formData.age}</td> <td>${formData.email}</td> </tr> </c:forEach> </table> </body> </html>
總結:
透過上述步驟,我們可以使用Java來寫一個簡單的表單資料來展示與瀏覽功能。透過建立一個FormData類別表示表單數據,然後編寫資料存取層程式碼與資料庫進行交互,最後將表單資料傳遞給JSP視圖進行展示。這樣,使用者就可以方便地查看和瀏覽已提交的表單資料了。
以上是使用Java編寫表單資料的展示與瀏覽功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!