当前位置:网站首页>EL & JSTL: JSTL summary
EL & JSTL: JSTL summary
2022-07-24 22:17:00 【dengfengling999】
Catalog :
(1)set Label use 1
(2)set Labels and EL Expression of
(3)if label
(4)choose label It is equivalent to multiple judgment statements
(5)forEach loop
(6)forEach Traverse List aggregate
(7)forEach Traverse Map aggregate
JSTL: Covered with labels java Tool class ,java Commands become labels , To meet the needs , Not allow java Command falls on jsp On
in order to jsp Can't see in the page java Code , Replace the original in jsp On the page <% java Code %>, A label is equivalent to a java command
Cause you :
Java Orders cannot fall on jsp The above to , So the java Put a label on the command , Package into a label to use
The other is EL Expression function is too weak

establish 04Model Import response jar package :


(1)set Label use 1

index.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 7:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set scope="application" var="sid" value="10"></c:set>
<c:set scope="session" var="sname" value="mike"></c:set>
<c:set scope="request" var="sage" value="26"></c:set>
<c:set scope="page" var="home" value=" Shanghai "></c:set>
Student number :${applicationScope.sid}<br>
Student name :${sessionScope.sname}<br>
Age of trainees :${requestScope.sage}<br>
Student's native place :${pageScope.home}
(2)set Labels and EL Expression of
index_2.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 8:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- Set the age of the user this year --%>
<c:set scope="request" var="age" value="20"></c:set>
<%-- Set the user's age after two years
Execution order , restrictive EL expression , Re execution JSTL,EL The expression starts with request Client reads data +2,
Then it is not written to the response body , It's about value Properties of the , And then execute set label ,
hold request scoped 20 Updated to 22
--%>
<c:set scope="request" var="age" value="${requestScope.age+2}"></c:set>
Two years later :${age}
(3)if label
index_3.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 8:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set scope="request" var="age" value="23"></c:set>
<c:if test="${sessionScope.age ge 18}">
<font color="red"> welcome </font>
</c:if>
<c:if test="${sessionScope.age lt 18}">
<font color="red"> Come back in two years </font>
</c:if>
Reported when running the project 500:
JSTL Label library Report 500 Error resolution
Cause analysis
Tomcat The lack of jstl-api-1.2.jar and standard-1.1.2.jar And these two jar package .
Solution
take jstl-api-1.2.jar and standard-1.1.2.jar These two bags are put into D:\Program\apache-tomcat-8.0.53\lib Under the table of contents 
Normal operation after joining :

(4)choose label It is equivalent to multiple judgment statements

index_4.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 9:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set scope="page" var="salary" value="15000"></c:set>
<c:choose>
<c:when test="${salary ge 50000}"> High wages </c:when>
<c:when test="${salary ge 20000}"> Normal wage </c:when>
<c:when test="${salary ge 10000}"> Low wages </c:when>
<c:otherwise> Abnormal wages </c:otherwise>
</c:choose>

(5)forEach loop

index_5.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 9:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<select>
<%
for (int i=1;i<=5;i++){
%>
<option> The first <%=i%> page </option>
<%
}
%>
</select>
<hr>
JSTL Syntax replace the top java sentence <br>
<select>
<c:forEach var="i" begin="1" end="5" step="1">
<option> The first ${pageScope.i} page </option>
</c:forEach>
</select>

(6)forEach Traverse List aggregate

Student class :
package com.bjpowernode.model;
public class Student {
private Integer sid;
private String sname;
public Student(Integer sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
establish Servlet:SixServlet:
package com.bjpowernode.controller;
import com.bjpowernode.model.Student;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SixServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu=new Student(10,"mike");
Student stu2=new Student(20,"allen");
List list=new ArrayList();
list.add(stu);
list.add(stu2);
request.setAttribute("key",list);
request.getRequestDispatcher("/index_6.jsp").forward(request,response);
}
}
stay web.xml: Specify the access method :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SixServlet</servlet-name>
<servlet-class>com.bjpowernode.controller.SixServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SixServlet</servlet-name>
<url-pattern>/six.do</url-pattern>
</servlet-mapping>
</web-app>index_6.jsp:
<%@ page import="java.util.List" %>
<%@ page import="com.bjpowernode.model.Student" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 10:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
java Command to get data <br>
<table border="2">
<tr>
<td> Student number </td>
<td> Student name </td>
</tr>
<%
List<Student> stuList=(List)request.getAttribute("key");
for(Student stu:stuList){
%>
<tr>
<td><%=stu.getSid()%></td>
<td><%=stu.getSname()%></td>
</tr>
<%
}
%>
</table>
<hr>
adopt JSTL Tag to get the data display <br>
<table border="2">
<tr>
<td> Student number </td>
<td> Student name </td>
</tr>
<c:forEach items="${requestScope.key}" var="stu">
<tr>
<td>${stu.sid}</td>
<td>${stu.sname}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

(7)forEach Traverse Map aggregate
controller :SixServlet:
package com.bjpowernode.controller;
import com.bjpowernode.model.Student;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SixServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu=new Student(10,"mike");
Student stu2=new Student(20,"allen");
//List aggregate
List list=new ArrayList();
list.add(stu);
list.add(stu2);
//Map aggregate
Map map=new HashMap();
map.put(" Class one ",stu);
map.put(" Class two ",stu2);
request.setAttribute("mapKey",map);
request.setAttribute("key",list);
request.getRequestDispatcher("/index_6.jsp").forward(request,response);
}
}
index_6.jsp:
<%@ page import="java.util.List" %>
<%@ page import="com.bjpowernode.model.Student" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 10:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%-- Need to add jstl The label of --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
java Command to get data <br>
<table border="2">
<tr>
<td> Student number </td>
<td> Student name </td>
</tr>
<%
List<Student> stuList=(List)request.getAttribute("key");
for(Student stu:stuList){
%>
<tr>
<td><%=stu.getSid()%></td>
<td><%=stu.getSname()%></td>
</tr>
<%
}
%>
</table>
<hr>
adopt JSTL Tag to get the data display <br>
<table border="2">
<tr>
<td> Student number </td>
<td> Student name </td>
</tr>
<c:forEach items="${requestScope.key}" var="stu">
<tr>
<td>${stu.sid}</td>
<td>${stu.sname}</td>
</tr>
</c:forEach>
</table>
<hr>
<h1> Traverse map aggregate </h1>
<table border="2">
<tr>
<td> Class name </td>
<td> Student number </td>
<td> Student name </td>
</tr>
<%-- Traverse Map Set every time from Map What you get from the set is a 【 Key value pair 】
And then 【 Key value pair 】 Give it to the loop variable
Loop variable .key get 【 Key value pair 】 Keyword name in Class name
Loop variable .value get 【 Key value pair 】 The content in stu object
--%>
<c:forEach items="${mapKey}" var="key_Value">
<tr>
<td>${key_Value.key}</td>
<td>${key_Value.value.sid}</td>
<td>${key_Value.value.sname}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

边栏推荐
- Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
- "Paper reproduction" bidaf code implementation process (4) model training + verification
- Microcomputer principle: detailed explanation of CPU architecture
- 吾爱第二课-去除网页弹窗
- Application programming of communication heartbeat signal for communication abnormality judgment
- Web3 security go + security
- Push information to wechat through enterprise wechat self built application
- Enterprise operation we media can't "self Hi": the content should be grounded, not advertising
- Gradle学习集合整合
- SVM - for linear separability (Part 2)
猜你喜欢

Feeding Program Source Code to ZK VMs

Gradle learning - integration of gradle and idea

由斐波那契数列引述到矩阵快速幂技巧

Local data enhancement method of icml2022 | graph neural network

ICML2022 | 图神经网络的局域数据增强方法

Maixll dock QR code recognition
![[which is better to use, apopost or apifox? Just read this!]](/img/58/4dfe5c56d12e8e88b0a7f3583ff0a4.jpg)
[which is better to use, apopost or apifox? Just read this!]

从暴力递归到动态规划,记忆化搜索

Kubernetes v1.24 is deployed based on containerd

集成Swagger 学习
随机推荐
Update structure of maximum or minimum value in the window - maximum value in the window
RISC0:Towards a Unified Compilation Framework for Zero Knowledge
C# 回看委托与事件
Homework of the 20th week
通讯异常判断之通讯心跳信号应用编程
Deep understanding of affairs
PCL point cloud processing: creating a two-dimensional grid to organize point cloud data (64)
集成Swagger 学习
实现redis哨兵,模拟master故障场景
一种兼容、更小、易用的WEB字体API
Random sampling and thinning of PCL point cloud processing randomsample (60)
Enterprise operation we media can't "self Hi": the content should be grounded, not advertising
ASP. Net core 6.0 data validation based on model validation
Web3安全 Go+Security
【数据库学习】Redis 解析器&&单线程&&模型
单调栈结构
From A76 to A78 -- learning arm microarchitecture in change
通过企业微信自建应用向微信推送信息
Clever use of sort (list & lt; T & gt;, comparator & lt;? Super T & gt;) comparator
Gradle learning set integration