目錄
一,基于JSP實作,JSTL 和 EL 表達式操作
1,任務概述
2,任務過程
3,參考過程
3.1 環境配置
3.2 項目結構
3.3 編碼過程(以添加商品為例,使用注解進行servlet配置,采用自頂向下的方法編寫)
3.4 結果展示
3.5 小技巧
一,基于JSP實作,JSTL 和 EL 表達式操作
1,任務概述
随着網際網路的發展,很多小企業的官網已經不局限于幾個靜态頁面,小璞 的公司最近就接到了一個項目,要做一家公司的官網,官網會不定時更新公司 産品資訊,并且潛在使用者還可以新增賬號成為公司的 VIP 使用者,如何實作這些 産品資訊的動态更新和展示?Html 已經遠遠不能滿足這個要求,你能用所學知識處理這個問題嗎?
簡單來說就是實作使用者注冊、使用者登入、産品資訊添加、産品資訊删除。。。(這描述太花哨了)
2,任務過程
- 1. 了解 JSP 頁面九大内置對象
- 2. 了解 JSP 三大指令的含義
- 3. 導入 JSTL 标簽庫
- 4. EL 表達式顯示資訊
3,參考過程
3.1 環境配置
PS:IDEA不要用太新的版本,比如2020版本,改動較大,很多地方的設定都和之前版本不同,我經常用的是2018.3.6版本(安裝包可以在最下方群檔案裡找到)。
1,修改IDEA編碼配置
2,在web\WEB-INF下建立classes、lib(注意将lib檔案夾的),并且修改相應配置(參考@ &再見螢火蟲&【06-JavaWEB_環境搭建——五,idea配置tomcat】);
3,添加Tomcat配置
配置完成如下:
4,servlet中的類extends HttpServlet飄紅?
看看External Libraries中有沒有引入Tomcat的包
如果沒有,可以在Project Structure/Modules/Dependencies中添加
3.2 項目結構
完整項目連結在這裡https://github.com/GoodbyeFirefly/JavaEngineerCourse/tree/main/TaskCards/Chapter7/JSPTaskCard
src目錄
web目錄(參考的小夥伴别忘了檢查下引入的jar包版本是否一緻,否則可能報錯,比如無法獲得資料庫連接配接等等)
3.3 編碼過程(以添加商品為例,使用注解進行servlet配置,采用自頂向下的方法編寫)
1,編寫index.jsp、productList.jsp頁面
使用者登入後跳轉到index.jsp頁面,在該頁面有一個超連結,點選超連結後向背景發送productList請求,背景接到請求後擷取到産品清單資訊,儲存、通過轉發跳轉到productList.jsp頁面;
productList.jsp可以展示産品資料,含有兩個按鈕實作産品的添加、删除(按鈕添加a标簽實作頁面跳轉);
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/productList" >展示産品清單</a>
</body>
</html>
productList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>産品清單</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
<td>産品名稱</td>
<td>産品價格</td>
<td>産品描述</td>
</tr>
<c:forEach var="product" items="${productList}">
<tr>
<td>${product.id}</td>
<td>${product.productName}</td>
<td>${product.price}</td>
<td>${product.description}</td>
</tr>
</c:forEach>
</table>
<button><a href="addProduct.jsp">添加産品</a></button>
<button><a href="delProduct.jsp">删除産品</a></button>
</body>
</html>
2,實作productListServlet
點選index.jsp中的a标簽時,發送productList請求,通過注解将productList請求和productListServlet進行綁定;
添加private ProductService productService = new ProductServiceImpl();表示提供産品相關的服務(這時由于還沒有實作相關的接口和實作類,會在編輯器視窗飄紅,不要害怕,後面會解決它);
通過服務擷取産品清單ArrayList list = productService.selectList();(類沒有實作,是以這個方法也是不存在的,會飄紅但是不要慌,後面會處理,這裡就先記下了)
package com.xrh.servlet;
import com.xrh.bean.Product;
import com.xrh.service.ProductService;
import com.xrh.service.impl.ProductServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/productList")
public class ProductListServlet extends HttpServlet {
private ProductService productService = new ProductServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ArrayList<Product> list = productService.selectList();
req.setAttribute("productList", list);// 通過setAttribute實作資料儲存
req.getRequestDispatcher("/productList.jsp").forward(req, resp);// 通過轉發方式跳轉頁面,避免資料丢失
}
}
3,在service包中完成接口和對應的實作類
實作類中涉及到和資料庫的互動,引入相應的dao類private ProductDao productDao = new ProductDaoImpl();(同樣會飄紅)
調用productDao.selectList();實作商品擷取(當然還是飄紅)
ProductService
package com.xrh.service;
import com.xrh.bean.Product;
import java.util.ArrayList;
public interface ProductService {
ArrayList<Product> selectList();
boolean addProduct(String productName, double price, String description);
boolean delProduct(int id);
}
ProductServiceImpl
package com.xrh.service.impl;
import com.xrh.bean.Product;
import com.xrh.dao.ProductDao;
import com.xrh.dao.impl.ProductDaoImpl;
import com.xrh.service.ProductService;
import java.util.ArrayList;
public class ProductServiceImpl implements ProductService {
private ProductDao productDao = new ProductDaoImpl();
@Override
public ArrayList<Product> selectList() {
return productDao.selectList();
}
@Override
public boolean addProduct(String productName, double price, String description) {
return productDao.addProduct(productName, price, description);
}
@Override
public boolean delProduct(int id) {
return productDao.delProduct(id);
}
}
4,在dao中完成對應的接口和實作類
完成對資料庫的相關操作
ProductDao
package com.xrh.dao;
import com.xrh.bean.Product;
import java.util.ArrayList;
public interface ProductDao {
ArrayList<Product> selectList();
boolean addProduct(String productName, double price, String description);
boolean delProduct(int id);
}
ProductDaoImpl
package com.xrh.dao.impl;
import com.xrh.bean.Product;
import com.xrh.dao.ProductDao;
import com.xrh.util.DruidUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
public class ProductDaoImpl implements ProductDao {
@Override
public ArrayList<Product> selectList() {
// Connection con = DruidUtil.getConnection();// DruidUtil中封裝了此方法,這裡不再需要
String sql = "select * from products";
ResultSet temp = null;
ArrayList<Product> result = new ArrayList<>();
try {
temp = DruidUtil.query(sql, null);
while (temp.next()) {
int id = temp.getInt("id");
String productName = temp.getString("productName");
double price = temp.getDouble("price");
String description = temp.getString("description");
result.add(new Product(id, productName, price, description));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DruidUtil.close();
}
return result;
}
@Override
public boolean addProduct(String productName, double price, String description) {
// Connection con = DruidUtil.getConnection();// DruidUtil中封裝了此方法,這裡不再需要
String sql = "insert products (productName, price, description) values(?, ?, ?)";
ArrayList<Object> params = new ArrayList<>(Arrays.asList(productName, price, description));
int result = 0;
try {
result = DruidUtil.update(sql, params);
} catch (Exception e) {
e.printStackTrace();
} finally {
DruidUtil.close();
}
return result == 0 ? false : true;
}
@Override
public boolean delProduct(int id) {
// Connection con = DruidUtil.getConnection();// DruidUtil中封裝了此方法,這裡不再需要
String sql = "delete from products where id=?";
ArrayList<Object> params = new ArrayList<>(Arrays.asList(id));
int result = 0;
try {
result = DruidUtil.update(sql, params);
} catch (Exception e) {
e.printStackTrace();
} finally {
DruidUtil.close();
}
return result == 0 ? false : true;
}
}
5,重複上面流程完成産品的添加功能(jsp=》servlet=》service=》dao)
産品添加的一種實作思路:通過産品清單下的按鈕,跳轉到産品添加的表單頁面,填寫完相關資訊後送出即可上傳到資料庫,點選傳回清單,就能傳回到産品清單中觀察到變化;
3.4 結果展示
低情商:不想搞前端
高情商:專注于背景功能實作
3.5 小技巧
如何自動生成資料庫表對應的bean類?
package com.xrh.bean;
public class Users {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}