标签化,可以简化百分号的繁忙。
标签库配置
先下载好jstl标签库,然后放到lib。接着,要让编译器识别到,因此要在模块配置依赖,这里导jar包都得注意一下模块依赖。
也可以在libraries项目库设置。
EL与JSTL查询图书
在数据库Book中建立表Tbook,包含图书ID,图书名称,图书价格。实现在bookQuery.jsp页面中模糊查询图书,如果图书的价格在50元以上,则以黄色字体显示书名。相应的模糊查询放在Servlet中实现,bookQuery.jsp页面中图书的显示代码注意使用JSTL。
这里主要用了mvc框架、数据库访问与模糊查询、EL与JSTL标签的使用。
package Beans;public class Book {private int bookId;private String bookName;private double bookPrice;public Book(int bookId, String bookName, double bookPrice) {this.bookId = bookId;this.bookName = bookName;this.bookPrice = bookPrice;}public int getBookId() {return bookId;}public void setBookId(int bookId) {this.bookId = bookId;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public double getBookPrice() {return bookPrice;}public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice;}}
package Dao;import Beans.Book;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class BookDao {private static final String URL = "jdbc:mysql://localhost:3306/dbjsp?serverTimezone=UTC";private static final String USERNAME = "root";private static final String PASSWORD = "123456";public List<Book> searchBooks(String s) {List<Book> books = new ArrayList<>();//查询的书可能有多个,用arraylist集try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}Connection conn = null;PreparedStatement statement = null;ResultSet rs = null;try {conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);String sql = "SELECT * FROM book WHERE Name LIKE ?";statement = conn.prepareStatement(sql);statement.setString(1, "%" + s + "%");rs = statement.executeQuery();while (rs.next()) {int bookId = rs.getInt("Id");String bookName = rs.getString("Name");double bookPrice = rs.getDouble("Price");books.add(new Book(bookId, bookName, bookPrice));}} catch (SQLException e) {e.printStackTrace();} finally {try {if (rs != null) {rs.close();}if (statement != null) {statement.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}return books;}
}
package Servlet;import Beans.Book;
import Dao.BookDao;import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/BookQuery")
public class BookQuery extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String searchKeyword = request.getParameter("keyword");BookDao bookDAO = new BookDao();List<Book> books = bookDAO.searchBooks(searchKeyword);//实现模糊查询request.setAttribute("books", books);request.getRequestDispatcher("bookQuery.jsp").forward(request, response);}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Book Query</title>
</head>
<style>table {width: 230px;}th, td,tr{border: none; /* 清除内部表格的边框 */}caption {font-weight: bold;}
</style>
<body><form action="BookQuery" method="post"><label for="keyword">请输入图书信息的模糊图书名称:</label><input type="text" id="keyword" name="keyword"><input type="submit" value="查询">
</form>
<br>
<table border="3px"><caption >图书列表</caption><tr><th>图书名称</th><th>图书价格</th></tr><c:forEach items="${books}" var="book"><tr><td <c:if test="${book.bookPrice > 50}">style="color: yellow"</c:if>>${book.bookName}</td><td>${book.bookPrice}</td></tr></c:forEach></table>
</body>
</html>
实验心得
过不去的是回想与停留。