0
点赞
收藏
分享

微信扫一扫

EL表达式与JSTL

IT程序员 2022-04-18 阅读 70
javahtml

目录

EL表达式

 JSTL基本标

EL表达式

实例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<%
	application.setAttribute("msg", "hello world");
	session.setAttribute("msg1", "hello");
	request.setAttribute("msg2", "world");
	pageContext.setAttribute("msg3", "world hello");
%>
<h1>
<!-- 
application:${applicationScope.msg}
session:${sessionScope.msg1}
request:${requestScope.msg2}
pageContext:${pageScope.msg3}
相当于底下的代码
 -->
application:${msg}<br>
session:${msg1}<br>
request:${msg2}<br>
pageContext:${msg3}<br>
</h1>
</body>
</html>

 效果如下:

 

 如果Attribute的命名相同,则只会返回最小的域对象的值:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<%
	application.setAttribute("msg", "hello world");
	session.setAttribute("msg", "hello");
	request.setAttribute("msg", "world");
	pageContext.setAttribute("msg", "world hello");
%>
<h1>
<!-- 
如果Attribute的命名相同,则只会返回最小的域对象的值
 -->
application:${msg}<br>
session:${msg}<br>
request:${msg}<br>
pageContext:${msg}<br>
</h1>
</body>
</html>

 效果如下:

 

 JSTL基本标签

<%@page import="java.util.ArrayList"%>
<%@page import="com.pojo.User"%>
<%@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>
<html>
<head>
<meta charset="UTF-8">
<title>绥彼岸</title>
</head>
<body>
<%
	List<User> list=new ArrayList<>();
	//循环给数组设置值
	for(int i = 0; i <= 3; i++){
		User user=new User();
		user.setUserName("userName"+i);
		user.setUserPwd("userPwd"+i);
		list.add(user);
	}
	//将数组添加到域对象当中去
	pageContext.setAttribute("list", list);
%>

<!-- 分别为set、out、remove -->
<c:set scope="page" var="msg" value="hello world"></c:set>
<c:out value="${msg}"></c:out>
<c:remove var="msg" scope="page"/>

<!-- if标签 -->
<c:if test="true">
<h1>hello world</h1>

<!-- forEach标签 -->
<c:forEach var="user" items="${list}">
<h1>
${user.userName}<br>
${user.userPwd}<br>
</h1>
</c:forEach>
</c:if>
</body>
</html>

 效果如下:

 

以上就是Java JSP    EL表达式与JSTL的一些内容!!

JAR 包可以私信博主

 感 谢 阅 读 ……

 

举报

相关推荐

EL表达式与JSTL标签库

el表达式+jstl标签

JSP(EL表达式+JSTL)

jstl&EL表达式

EL表达式JSTL标签

JavaWeb 16 EL表达式与JSTL

EL表达式&JSTL学习笔记

0 条评论