一文帶你吃透JSP增刪改查實(shí)戰(zhàn)案例詳細(xì)解讀
前言
不得不說,JSP 現(xiàn)在已經(jīng)是一門十分老舊的技術(shù)了,學(xué)習(xí)編程時(shí),不僅要學(xué)習(xí)優(yōu)秀的前言技術(shù),還要對(duì)基礎(chǔ)有一定的把握,所以學(xué)習(xí) JSP 時(shí),我們只做了解,不用刨根問底花費(fèi)大量的時(shí)間,得不償失。
我們主要從以下幾個(gè)方面學(xué)習(xí) JSP 技術(shù):
- 理解 JSP 及其原理
- 學(xué)會(huì)使用 EL 表達(dá)式和 JSTL 標(biāo)簽
- 理解 MVC 模式和三層架構(gòu)(重點(diǎn))
學(xué)習(xí) JSP 到什么程度呢?我們只需要能夠使用 JSP 相關(guān)技術(shù)能夠?qū)崿F(xiàn)簡單數(shù)據(jù)的增刪改查即可。
JSP 概述
JSP,Java Server Pages,指的是 Java 服務(wù)端頁面,是一種動(dòng)態(tài)的網(wǎng)頁技術(shù),其中既可以定義 HTML,CSS,JavaScript 靜態(tài)內(nèi)容,也可以使用 Java 代碼定義動(dòng)態(tài)內(nèi)容。通俗的說,JSP = HTML + Java 。下面就是一個(gè)最簡單的 JSP 代碼:
<html>
<head>
<title>title</title>
</head>
<body>
<h1>HelloWorld</h1>
<%
System.out.println("HelloWorld!");
%>
</body>
</html>
上面的例子中,html 里面的內(nèi)容會(huì)在瀏覽器渲染,而 Java 代碼的輸出語句會(huì)打印在 idea 控制臺(tái)。
那么 JSP 到底是用來做什么的?我們?nèi)绾螌?shí)現(xiàn)不同用戶登錄網(wǎng)頁顯示不同的信息呢?例如,在頁面上顯示登錄的用戶名:

在 Servlet 中,我們使用 write 對(duì)象向?yàn)g覽器寫數(shù)據(jù),html 標(biāo)簽可以作為字符串傳入 write() 方法中。但是,當(dāng)向頁面寫出大量數(shù)據(jù)時(shí),代碼顯得十分的混亂不堪,并且后期后期維護(hù)成本極大。而 JSP 就結(jié)局了這個(gè)問題,在 JSP 代碼中可以直接使用 html 標(biāo)簽,動(dòng)態(tài)數(shù)據(jù)也可以使用 Java 代碼展示,大大的簡化了開發(fā),避免了在 Servlet 中直接輸出 html 標(biāo)簽。
JSP快速入門
搭建環(huán)境
在 idea 中創(chuàng)建一個(gè) Maven Web 項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:

在 pom.xml 添加 Servlet 的依賴坐標(biāo)和 TomCat 插件坐標(biāo),如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.chengzi</groupId>
<artifactId>jsp-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jsp-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
<finalName>jsp-demo</finalName>
</build>
</project>
導(dǎo)入JSP依賴
在 pom.xml 中添加 JSP 的依賴坐標(biāo),如下:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
創(chuàng)建 JSP 頁面
在項(xiàng)目的 webapps 文件目錄下創(chuàng)建 jsp 頁面,如圖:

通過上面的操作創(chuàng)建一個(gè)名為 hello.jsp 的頁面。
編寫代碼
在 hello.jsp 中書寫 HTML 標(biāo)簽和 Java 代碼,如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>HelloWorld</h1>
<%
System.out.println("helloWorld!");
%>
</body>
</html>測(cè)試
啟動(dòng) Tomcat 服務(wù)器,在瀏覽器訪問 http://localhost:8080/jsp-demo/hello.jsp,結(jié)果如下:

JSP原理
之前,我們?cè)?jsp 中不僅編寫了 html 標(biāo)簽,還編寫了 Java 代碼,為什么呢?其實(shí),JSP 本質(zhì)上就是一個(gè) Servlet。要弄明白這個(gè)問題,我們就要研究 jsp 的執(zhí)行流程。

在上面的例子中,瀏覽器第一次訪問 heelo.jsp 頁面時(shí),Tomcat 會(huì)將 hello.jsp 裝換為名為 hello_jsp.java 的一個(gè)Servlet, 然后 這個(gè) Servlet 會(huì)被 TomCat 編譯為字節(jié)碼文件 hello_jsp.class 。TomCat 會(huì)執(zhí)行該字節(jié)碼文件向外提供服務(wù)。
此時(shí)在項(xiàng)目磁盤目錄下找到 target\tomcat\work\Tomcat\localhost\jsp-demo\org\apache\jsp ,在這個(gè)文件目錄下就可以看到裝換后的 Servlet 和編譯后的 .class 文件。如圖:

打開 hello_jsp.java 文件查看源碼,如圖:

可以發(fā)現(xiàn)轉(zhuǎn)換后的 hello_jsp 類繼承自 HttpJspBase ,其實(shí) HttpJspBase 類是繼承自 HttpServlet 的,我們可以查看 TomCat 源碼,所以 hello_jsp 類間接繼承了 HttpServlet ,也就是說,JSP 容器將 JSP 文件裝換成了一個(gè) Servlet。
在 hello_jsp 類中還有一個(gè) _jspService() 方法,該方法和 Servlet 中的 service 方法類似,在每次訪問警示牌 jsp 時(shí)自動(dòng)執(zhí)行。如圖:

在 _jspService() 方法中可以看到完瀏覽器中寫的標(biāo)簽和 Java 代碼,如下:

不難看出,在 <%...%> 中的 Java 代碼被放到了 _jspService() 方法中,這段 Java 代碼被稱為 JSP 腳本。
JSP 腳本
JSP 腳本用于在 JSP 頁面內(nèi)定義 Java 代碼,之前案例中使用到的 Java 代碼就是定義在 JSP 腳本中的,JSP 腳本一共分為 3 類:
- <%…%> 內(nèi)容會(huì)直接放到 _jspService() 方法中
- <%=…%> 內(nèi)容或被放到 out.print() 中,作為該的方法的參數(shù)
- <%!..%> 內(nèi)容會(huì)被放到 _jspService() 方法之外,被類直接包括
代碼演示:
第一步:在 hello.jsp 中編寫代碼:
<%
System.out.println("hello");
int i = 3;
%>
通過瀏覽器訪問該資源,查看轉(zhuǎn)換后的 hello_jsp.java 源代碼,不難發(fā)現(xiàn) i 變量被定義在 _jspService() 方法中,如圖:
第二步:在 hello.jsp 中編寫代碼:
<%="hello"%> <%=i%>
通過瀏覽器訪問該資源,查看轉(zhuǎn)換后的 hello_jsp.java 源代碼,不難發(fā)現(xiàn) i 變量成為了 out.print() 方法的參數(shù),并且這兩個(gè)數(shù)據(jù)被發(fā)送到了瀏覽器展示給用戶,如圖:

第三步:在 hello.jsp 中編寫代碼:
<%!
void show(){}
String name = "zhangsan";
%>
通過瀏覽器訪問該資源,查看轉(zhuǎn)換后的 hello_jsp.java 源代碼,不難發(fā)現(xiàn)該部分被放到了類的成員部分,如圖:

實(shí)戰(zhàn)案例
使用 JSP 在瀏覽器以表格的形式展示學(xué)生信息的數(shù)據(jù),這里的數(shù)據(jù)應(yīng)該使用 MyBatis 在數(shù)據(jù)庫中查找,但是這里只是簡單的演示 JSP 的使用,所以我們省略了對(duì)數(shù)據(jù)庫的操作,直接將數(shù)據(jù)封裝到 Student 對(duì)象中并存放在集合中。
首先我們?cè)?org.chengzi.pojo 包下創(chuàng)建一個(gè)實(shí)體類 Student 。在 Java 文件目錄右擊 new / Java class ,創(chuàng)建包和類。如下:
public class Student {
private int id;
private String name;
private String gender;
private int scoreEnglish;
private int scoreMath;
//省略了getter和setter方法以及重寫的Object類的toString方法
}
在項(xiàng)目 webapps 目錄下創(chuàng)建 student.jsp,在此文件中寫 html 標(biāo)簽和 Java 代碼,我們可以先將數(shù)據(jù)寫死,方便展示在瀏覽器。如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="新增">
<hr>
<table border="1" cellspacing="0" width="800">
<tr>
<th>序號(hào)</th>
<th>姓名</th>
<th>性別</th>
<th>英語成績</th>
<th>數(shù)學(xué)成績</th>
<th>操作</th>
</tr>
<tr align="center">
<td>1</td>
<td>張三</td>
<td>男</td>
<td>100</td>
<td>100</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a></td>
</tr>
<tr align="center">
<td>2</td>
<td>李四</td>
<td>男</td>
<td>100</td>
<td>100</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a></td>
</tr>
<tr align="center">
<td>3</td>
<td>王五</td>
<td>女</td>
<td>100</td>
<td>100</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a></td>
</tr>
</table>
</body>
</html>
在瀏覽器訪問該資源,效果如下:

準(zhǔn)備需要?jiǎng)討B(tài)展示到瀏覽器的數(shù)據(jù),這里省略了從數(shù)據(jù)庫查詢數(shù)據(jù),我們直接將數(shù)據(jù)封裝在 Student 對(duì)象中,并將所有對(duì)象放在集合中。例如:
<%
// 查詢數(shù)據(jù)庫
List<Student> brands = new ArrayList<Student>();
brands.add(new Student(1,小張,女,85,89));
brands.add(new Student(2,小樊,女,98,87));
brands.add(new Student(3,小李,男,100,99));
%>
上面我們使用固定的數(shù)據(jù)發(fā)送到瀏覽器展示,為了動(dòng)態(tài)的展示數(shù)據(jù),我們要使用 Java 代碼來動(dòng)態(tài)的獲取數(shù)據(jù),而在 JSP 頁面中,Java 代碼要寫在 JSP 腳本中,如下:
<%@ page import="org.chengzi.pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// 查詢數(shù)據(jù)庫
List<Student> students = new ArrayList<Student>();
students.add(new Student(1, "小張", "男", 80, 85));
students.add(new Student(2, "小李", "女", 98, 87));
students.add(new Student(3, "小樊", "女", 98, 100));
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="新增">
<hr>
<table border="1" cellspacing="0" width="800">
<tr>
<th>序號(hào)</th>
<th>姓名</th>
<th>性別</th>
<th>英語成績</th>
<th>數(shù)學(xué)成績</th>
<th>操作</th>
</tr>
<%
for (int i = 0; i < students.size(); i++) {
//獲取集合中的 每一個(gè) Brand 對(duì)象
Student student = students.get(i);
%>
<tr align="center">
<td><%=student.getId()%>
</td>
<td><%=student.getName()%>
</td>
<td><%=student.getGender()%>
</td>
<td><%=student.getScoreEnglish()%>
</td>
<td><%=student.getScoreMath()%>
</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
此時(shí),數(shù)據(jù)就會(huì)被動(dòng)態(tài)的展示到瀏覽器頁面,不難看出,其實(shí) JSP 腳本標(biāo)簽就是在分割 Java 代碼,在標(biāo)簽內(nèi)是 Java 代碼,在標(biāo)簽外則是 html 語句,如圖:

JSP缺點(diǎn)
不難看出,JSP 開發(fā)還是有很多的缺點(diǎn)的,首先,既然在 jsp 文件中既可以寫 html 標(biāo)簽,有可以寫 Java 代碼,導(dǎo)致了書寫麻煩和閱讀麻煩的問題。并且其運(yùn)行要依賴各種環(huán)境,例如 jre,jsp 容器,JavaEE 等等,導(dǎo)致其程序變得十分復(fù)雜。
JSP 會(huì)自動(dòng)生成 .java 文件和 .class 文件占用了磁盤空間,而運(yùn)行 .class 文件占用內(nèi)存。在程序出錯(cuò)以后,我們一般要找到自動(dòng)生成的 .java 文件進(jìn)行調(diào)試,導(dǎo)致了調(diào)試?yán)щy的問題。
另外,把 Java 代碼和 html 代碼混在一個(gè)文件中是非常不利于團(tuán)隊(duì)開發(fā)的,如果頁面出現(xiàn)問題,首先前端開發(fā)工程師修改靜態(tài)頁面,然后由后端開發(fā)來將該頁面修改為 JSP 頁面,這不是我們想看到的合作開發(fā)方式。
由于 JSP 存在著很多的確定,并且隨著軟件技術(shù)的發(fā)展,JSP 已經(jīng)逐漸退出歷史的舞臺(tái)了,后面我們會(huì)學(xué)習(xí)新的技術(shù) AJAX 來替代前面的方式,有了這個(gè)技術(shù)以后,前端工程師只負(fù)責(zé)前端頁面的開發(fā),而后端工程師只負(fù)責(zé)后端代碼的開發(fā),職責(zé)單一高效。
當(dāng)然,我們并不是說后端開發(fā)就不用學(xué)習(xí)前端的知識(shí),即使你是專注于后端開發(fā),前端的知識(shí)你同樣要了解,任何一個(gè)說后端不用了解前端知識(shí)的說法都是不負(fù)責(zé)任大額。
發(fā)展階段
下面我們看一下服務(wù)器頁面技術(shù)的發(fā)展過程,聊聊為什么 JSP 既然已經(jīng)被淘汰,為什么我們還要學(xué)習(xí)呢?

第一階段:
使用 Servlet 技術(shù)實(shí)現(xiàn)邏輯代碼的編寫,也對(duì)頁面進(jìn)行拼接。
第二階段:
隨著技術(shù)的發(fā)展,出現(xiàn)了 JSP,JSP 確實(shí)使用比 Servlet 方便了很多,但是隨著時(shí)間的發(fā)展,JSP 的很多確定都暴露出來了。
第三階段:
使用 Servlet 進(jìn)行邏輯開發(fā),使用 JSP 進(jìn)行數(shù)據(jù)的展示。

第四階段:
使用 Servlet 進(jìn)行后端邏輯代碼的開發(fā),使用 HTML 進(jìn)行數(shù)據(jù)的展示,html 是靜態(tài)頁面,為了展示動(dòng)態(tài)數(shù)據(jù),出現(xiàn)了新的技術(shù) AJAX。
現(xiàn)在我們學(xué)習(xí) JSP 時(shí),大概有兩個(gè)原因,一是一些公司的老項(xiàng)目仍然使用了 JSP,所以為了項(xiàng)目的維護(hù),我們不得不學(xué)習(xí),另外,學(xué)習(xí)技術(shù)是一個(gè)循序漸進(jìn)的過程,我們必須一步一步的學(xué)習(xí),雖然 JSP 老舊過時(shí),但是其中的編程思想使我們學(xué)習(xí)編程過程中應(yīng)該了解的。
前面說到,使用 Servlet 進(jìn)行邏輯開發(fā),使用 JSP 來獲取數(shù)據(jù),遍歷展現(xiàn)數(shù)據(jù),這樣的方式解決了 JSP 頁面中 Java 代碼混亂的問題,接下來看一看如何使用 EL 表達(dá)式和 JSTL 標(biāo)簽庫來替代 JSP 頁面中的 Java 代碼。
EL 表達(dá)式
概述
EL ,Expression Language,稱為表達(dá)式語言,用于簡化 JSP 頁面內(nèi)的 Java 代碼。EL 表達(dá)式的主要作用是獲取數(shù)據(jù),期數(shù)就是從域?qū)ο笾蝎@取數(shù)據(jù),然后將數(shù)據(jù)展示在頁面上。
EL 表達(dá)式的語法比較簡單,使用${expression},例如 ${student} 就是獲取域?qū)ο笾写鎯?chǔ)的 key 為 student 的數(shù)據(jù)。
實(shí)戰(zhàn)案例
定義一個(gè) Servlet,在 Servlet 中封裝數(shù)據(jù)并存儲(chǔ)到 request 域?qū)ο笾胁⑥D(zhuǎn)發(fā)到 el-demo.jsp 頁面。
@WebServlet("/demo")
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//準(zhǔn)備數(shù)據(jù)
List<Student> students = new ArrayList<Student>();
students.add(new Student(1,"張三","男",100,100));
students.add(new Student(2,"李四","女",98,98));
//存儲(chǔ)到request域?qū)ο笾校ㄊ褂胷equest轉(zhuǎn)發(fā),將request作為域?qū)ο螅?
request.setAttribute("students",students);
//發(fā)送到 el-demo.jsp 頁面
request.getRequestDispatcher("/el-demo.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
在 el-demo.jsp 中通過 EL 表達(dá)式來獲取數(shù)據(jù),EL 表示式后面不需要加分號(hào)。例如:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${students}
</body>
</html>
如果 JSP 中沒有接收到 Servlet 共享的數(shù)據(jù),如圖:

解決方法:在 JSP 代碼中添加:
isELIgnored="false"
添加位置在:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
域?qū)ο?/h2>
上面我們?cè)?Servlet 中使用轉(zhuǎn)發(fā)將 request 作為域?qū)ο筮M(jìn)行數(shù)據(jù)共享,那么什么是域?qū)ο??域?qū)ο笥心男┠兀?/p>
在 JavaWeb 中,一共有四大域?qū)ο?,分別是:
- page:當(dāng)前頁有效
- request:當(dāng)前請(qǐng)求有效
- session:當(dāng)前會(huì)話有效(后面聊什么是會(huì)話)
- application:當(dāng)前應(yīng)用有效
如圖所示:

EL 表達(dá)式獲取數(shù)據(jù),會(huì)一次從四個(gè)域中尋找,知道找到為止。
例如,例如 EL 表達(dá)式 ${students} 會(huì)先從 page 域?qū)ο笾蝎@取數(shù)據(jù),如果沒有找到,再去 request 域?qū)ο笾蝎@取數(shù)據(jù),一次尋找,直到找到數(shù)據(jù)。
JSTL 標(biāo)簽
前端開發(fā)工程師每天都在和標(biāo)簽打交道,他們對(duì)標(biāo)簽是更加敏感的,所以出現(xiàn)了 JSTL 標(biāo)準(zhǔn)標(biāo)簽庫,使用標(biāo)簽替代了 JSP 頁面中的 Java 代碼,提高了代碼的可讀性。如下:
<c:if test="${flag == 1}">
男
</c:if>
<c:if test="${flag == 2}">
女
</c:if>
JSTL,Jsp Standarded Tag Library,JSP 標(biāo)準(zhǔn)標(biāo)簽庫,其中提供了豐富的標(biāo)簽用于取代 JSP 頁面中的 Java 代碼,例如:
| 標(biāo)簽 | 描述 |
|---|---|
| <c:out> | 用于在JSP中顯示數(shù)據(jù),就像<%= … > |
| <c:set> | 用于保存數(shù)據(jù) |
| <c:remove> | 用于刪除數(shù)據(jù) |
| <c:catch> | 用來處理產(chǎn)生錯(cuò)誤的異常狀況,并且將錯(cuò)誤信息儲(chǔ)存起來 |
| <c:if> | 與我們?cè)谝话愠绦蛑杏玫膇f一樣 |
| <c:choose> | 本身只當(dāng)做<c:when>和<c:otherwise>的父標(biāo)簽 |
| <c:when> | <c:choose>的子標(biāo)簽,用來判斷條件是否成立 |
| <c:otherwise> | <c:choose>的子標(biāo)簽,接在<c:when>標(biāo)簽后,當(dāng)<c:when>標(biāo)簽判斷為false時(shí)被執(zhí)行 |
| <c:import> | 檢索一個(gè)絕對(duì)或相對(duì) URL,然后將其內(nèi)容暴露給頁面 |
| <c:forEach> | 基礎(chǔ)迭代標(biāo)簽,接受多種集合類型 |
| <c:forTokens> | 根據(jù)指定的分隔符來分隔內(nèi)容并迭代輸出 |
| <c:param> | 用來給包含或重定向的頁面?zhèn)鬟f參數(shù) |
| <c:redirect> | 重定向至一個(gè)新的URL. |
| <c:url> | 使用可選的查詢參數(shù)來創(chuàng)造一個(gè)URL |
使用 JSTL 之前要先導(dǎo)入依賴和引入標(biāo)簽庫,例如在 pom.xml 中添加依賴坐標(biāo):
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
在 JSP 頁面中引入 JSTL 標(biāo)簽庫:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
下面舉例練習(xí)幾個(gè)常用的標(biāo)簽。
<c:if>標(biāo)簽相當(dāng)于 Java 中的if判斷語句,示例:定義一個(gè) Servlet,在 Servlet 中向 request 域?qū)ο笾刑砑渔I為 status,值為 1 的數(shù)據(jù)。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存儲(chǔ)數(shù)據(jù)到request域?qū)ο笾?
request.setAttribute("status",1);
//發(fā)送到 jstl.jsp頁面
request.getRequestDispatcher("/jstl.jsp").forward(request,response);
}
定義 jstl.jsp 頁面,使用 <c:if> 標(biāo)簽進(jìn)行判斷:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
c:if:來完成邏輯判斷,替換java if else
--%>
<c:if test="${status ==1}">
啟用
</c:if>
<c:if test="${status ==0}">
禁用
</c:if>
</body>
</html>
<forEach>標(biāo)簽相當(dāng)于 Java 中的 for 循環(huán),Java 中有普通 for 循環(huán)和增強(qiáng) for 循環(huán),其實(shí)在 JSTL 中的循環(huán)標(biāo)簽也有兩種用法:
用法1
第一種用法類似于 Java 中的普通 for 循環(huán),其中主要使用到的屬性有:
begin:開始數(shù)
end:結(jié)束數(shù)
step:步長
var:循環(huán)變量
示例:
<c:forEach begin="0" end="10" step="1" var="i"> ${i} </c:forEach>
用法2
第二種用法類似于 Java 中的增強(qiáng) for 循環(huán),主要使用的屬性有:
- items:被遍歷的容器
- var:遍歷產(chǎn)生的零時(shí)變量
- varStatus:遍歷狀態(tài)對(duì)象
示例:
<c:forEach items="${students}" var="student">
<tr align="center">
<td>${student.id}</td>
<td>${student.brandName}</td>
<td>${student.companyName}</td>
<td>${student.description}</td>
</tr>
</c:forEach>
上面例子中,首先從域?qū)ο笾蝎@取了 students 數(shù)據(jù),該數(shù)據(jù)是一個(gè)集合,遍歷結(jié)合,并給集合中每一個(gè)對(duì)象起名為 student ,在循環(huán)中使用了 EL 表達(dá)式獲取每一個(gè) Student 對(duì)象的屬性值。

以上就是一文帶你吃透JSP增刪改查實(shí)戰(zhàn)案例詳細(xì)解讀的詳細(xì)內(nèi)容,更多關(guān)于JSP增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)常見排序算法的優(yōu)化
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Java實(shí)現(xiàn)常見排序算法的優(yōu)化展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-01-01
java實(shí)現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
自定義spring mvc的json視圖實(shí)現(xiàn)思路解析
這篇文章主要介紹了自定義spring mvc的json視圖的實(shí)現(xiàn)思路解析,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
java并發(fā)編程工具類PriorityBlockingQueue優(yōu)先級(jí)隊(duì)列
這篇文章主要為大家介紹了java并發(fā)編程工具類PriorityBlockingQueue優(yōu)先級(jí)隊(duì)列的方法示例應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
小白也可以學(xué)會(huì)的Java NIO的Write事件
剛開始對(duì)NIO的寫操作理解的不深,不知道為什么要注冊(cè)寫事件,何時(shí)注冊(cè)寫事件,為什么寫完之后要取消注冊(cè)寫事件,今天特地整理了本篇文章,需要的朋友可以參考下2021-06-06
java算法導(dǎo)論之FloydWarshall算法實(shí)現(xiàn)代碼
這篇文章主要介紹了算法導(dǎo)論之FloydWarshall算法實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
springboot2.6.3讀取不到nacos上的配置文件問題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

