jsp+EL+JSTL模式

代碼倉庫地址+文檔:https://gitee.com/DerekAndroid/JspDemo.git

效果(左邊普通jsp,右邊JSTL模式jsp):

案例一:查詢所有商品信息

sql

		-- 簡易商品表
		CREATE TABLE product(
			id INT PRIMARY KEY AUTO_INCREMENT,
			pname VARCHAR(20),
			price DOUBLE,
			pdesc VARCHAR(20)
		);

		INSERT INTO product VALUES (NULL,'電視機',3200,'液晶曲面大電視');
		INSERT INTO product VALUES (NULL,'韭菜盒子',3,'味重請小心食用');
		INSERT INTO product VALUES (NULL,'益達',10,'韭菜伴侶');
		INSERT INTO product VALUES (NULL,'十三香',12,'守義牌');

 

核心代碼:servlet請求轉發到jsp 

package com.itheima.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.bean.Product;
import com.itheima.service.ProductService;

public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//創建ProductService
			ProductService ps = new ProductService();
			//調用查詢所有商品的方法
			List<Product> list=ps.findAllProducts();
			
			//把list放入reuqest域中
			request.setAttribute("list", list);
			//請求轉發到list.jsp中
			request.getRequestDispatcher("/list.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

list.jsp

<%@page import="com.itheima.bean.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<%
		List<Product> list = (List<Product>)request.getAttribute("list");
	%>
	
	<table border="1px" align="left">
		<tr>
			<th>商品id</th>
			<th>商品名稱</th>
			<th>商品價格</th>
			<th>商品描述</th>
		</tr>
		<%
			if(list==null){
				%>
				<tr>
					<td colspan="4">暫無商品</td>
				</tr>
				<%
			}else{
				for(Product pro:list){
					%>
					<tr>
						<td><%=pro.getId() %></td>
						<td><%=pro.getPname() %></td>
						<td><%=pro.getPrice() %></td>
						<td><%=pro.getPdesc() %></td>
					</tr>
					<%
				}
			}
		%>
	</table>
</body>
</html>

使用list-jstl.jsp

<%@page import="com.itheima.bean.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
	<table border="1px" align="left">
		<tr>
			<th>商品id</th>
			<th>商品名稱</th>
			<th>商品價格</th>
			<th>商品描述</th>
		</tr>

		<c:if test="${empty list}">
			<tr>
				<td colspan="4">暫無商品,請添加</td>
			</tr>
		</c:if>

		<c:if test="${not empty list}">
			<c:forEach items="${list}" var="pro">
				<tr>
					<td>${pro.id}</td>
					<td>${pro.pname}</td>
					<td>${pro.price}</td>
					<td>${pro.pdesc}</td>
				</tr>
			</c:forEach>
		</c:if>
	</table>
</body>
</html>

jsp運行原理 

jsp就是servlet

jsp四個域範圍

jsp總結

el表達式

JSTL就是標準標籤庫

el+jstl+jsp模式

發佈了178 篇原創文章 · 獲贊 44 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章