JSP(8)—EL案例和JSTL案例
1.EL案例
el.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.test.bean.Customer" %>
<%@ page import="java.lang.reflect.Method;" %>
<!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>
<!-- 一、使用JavaBean为属性赋值,属性的有效范围为session-->
<jsp:useBean id="customer1" class="com.test.bean.Customer" scope="session"></jsp:useBean>
<jsp:setProperty property="username" name="customer1" value="root"/>
<!-- 获取设置的属性值的三种方式 -->
<!-- 1.使用Java代码 -->
<%
Customer customer2 = (Customer)session.getAttribute("customer1");
out.println("1.username:");
out.println(customer2.getUsername());
%>
<br><br>
<!-- 2.使用JavaBean的getProperty()方法 -->
2.username:<jsp:getProperty property="username" name="customer1"/>
<br><br>
<!-- 3.使用EL表达式 -->
3.username:${sessionScope.customer1.username }
<br><br>
<!-- 二、获取请求参数的值 ${param.username }-->
<form action="el.jsp" method="post">
①回显——使用request对象
usernName:<input type="text" name="username"
value="<%=request.getParameter("username") == null ? "" : request.getParameter("username")%>">
<br><br>②回显——使用EL表达式:
usernName:<input type="text" name="username"
value="${param.username }">
<input type="submit" value="Submit">
<br><br>③使用request对象:
userName:<%=request.getParameter("username") %>
</form>
<br><br>
<!-- 三、跨页面传递参数 -->
<a href="el2.jsp?age=26&name=a&name=b&name=c">To El2 Page</a>
<br><br>
<!-- 四、存储数据范围 -->
<%
Customer customer = new Customer();
customer.setUsername("Ann");
request.setAttribute("customer1", customer);
%>
获取默认范围内的username的值:${customer1.username }
<br><br>
获取session范围内的username的值:${sessionScope.customer1.username }
</body>
</html>
el2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.test.bean.Customer" %>
<!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>
<!-- 五、自动类型变换 -->
age:${param.age }
<br><br>
age:<%=request.getParameter("age")%>
<br><br>
对数据进行加减操作之后:
<br><br>
age:${param.age + 10}
<br><br>
age:<%=request.getParameter("age") + 10%>
<br><br>
<!-- 六、跨页面获取属性值 -->
<!-- 获取设置的属性值的三种方式 -->
<!-- 1.使用Java代码 -->
<%
Customer customer3 = (Customer)session.getAttribute("customer1");
out.println("1.username:");
out.println(customer3.getUsername());
%>
<br><br>
<!-- 2.使用JavaBean的getProperty()方法 -->
<jsp:useBean id="customer1" class="com.test.bean.Customer" scope="session"></jsp:useBean>
2.username:<jsp:getProperty property="username" name="customer1"/>
<br><br>
<!-- 3.使用EL表达式 -->
3.username:${sessionScope.customer1.username }
<br><br>
<!-- 七、隐含对象 -->
<!-- 1、与范围相关的隐含对象pageScope、requestScope、sessionScope、applicationScope -->
<%
application.setAttribute("username", "appName");
session.setAttribute("username", "sesName");
request.setAttribute("username", "reqName");
pageContext.setAttribute("username", "pageName");
%>
<br><br>
pageScope——username:${pageScope.username }
<br><br>
requestScope——username:${requestScope.username }
<br><br>
sessionScope——username:${sessionScope.username }
<br><br>
applicationScope——username:${applicationScope.username }
<br><br>
<!-- 2.与输入有关的隐含对象Param、ParamValues,与五中的自动类型变换获取属性的方式一样 -->
Age(param):${param.age }
<br><br>
Names(paramValues):${paramValues.name }
<br><br>
Names(paramValues):${paramValues.name[0] }
<br><br>
<!-- 3.其他隐含对象 cookie、header、headerValues、initParam、pageContext -->
cookie:${cookie.JSESSIONID.name }——${cookie.JSESSIONID.value }
<br><br>
header:${header["Accept-Language"] }
<br><br>
initParam:${initParam.user }
<br><br>
pageContext即为PageContext类型,但只能读取属性。
pageContext读取contextPath:${pageContext.request.contextPath }
<br><br>
sessionid:${pageContext.session.id }
<br><br>
<!-- 八、运算符 -->
年龄:${param.age },是${param.age > 18 ? "成年人" : "未成年人" }
<br><br>
<form action="ElFunction.jsp">
<input type="text" name="name" value="test">
<input type="submit" value="To ElFunction Page">
</form>
</body>
</html>
ELFunction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="test" uri="http://www.Test.com/mytag/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>
<h3>EL表达式函数</h3>
<!-- 1.使用预定义函数 -->
参数长度:${fn:length(param.name)}
<br><br>
大小写转换:${fn:toUpperCase(param.name) }
<!-- 2.自定义函数 -->
<br><br>
两数相加,和为:${test:concat("10", "10") }
</body>
</html>
2.JSTL案例
jstl.jsp
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.test.bean.Student"%>
<%@page import="com.test.bean.Customer"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ 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>
<%
request.setAttribute("book", "<<Java>>");
Customer customer = new Customer();
customer.setUsername("test");
request.setAttribute("cus", customer);
%>
<form action="jstl.jsp">
<input type="text" name="name" value="test">
<input type="text" name="age" value="23">
<input type="submit" value="Sunbmit">
</form>
<h3>jstl核心标签库,表达式操作</h3>
<!-- 1.out -->
book:${requestScope.book }
<br><br>
book:<c:out value="${requestScope.book}"></c:out>
<br><br>
<!-- 2.set -->
<c:set var="name" value="test" scope="page"></c:set>
<c:set var="name" value="test" scope="request"></c:set>
<c:set var="name" value="${param.name }" scope="session"></c:set>
name1:${pageScope.name }
<br><br>
name2:${requestScope.name }
<br><br>
name3:${sessionScope.name }
<br><br>
<!-- 把请求参数赋给javaBean的属性 -->
name4:${requestScope.cus.username }
<br><br>
<!-- 方案一、使用java代码 -->
<%
String str = request.getParameter("name");
request.setAttribute("username", str);
%>
<!-- 显示结果 -->
name5:${requestScope.cus.username }
<br><br>
<!-- 方案二、使用jstl标签 -->
<c:set target="${requestScope.cus }" property="username" value="${param.name }"></c:set>
<!-- 显示结果 -->
name6:${requestScope.cus.username }
<br><br>
<!-- 3.remove -->
<c:remove var="name" scope="session"/>
<!-- 显示结果 -->
name3(移除属性name3的结果显示):${sessionScope.name }
<br><br>
<h3>jstl核心标签库,流程控制操作</h3>
<!-- 4.if -->
<c:set var="age" value="20" scope="page"></c:set>
<c:if test="${pageScope.age > 18}">成年</c:if>
<br><br>
<c:if test="${pageScope.age > 18}" var="isAdult" scope="page"></c:if>
isAudlt:<c:out value="${pageScope.isAdult}"></c:out>
<br><br>
<!-- 5.choose when otherwise -->
<c:choose>
<c:when test="${param.age > 60 }">老年</c:when>
<c:when test="${param.age > 30 }">中年</c:when>
<c:when test="${param.age > 18 }">青年</c:when>
<c:when test="${param.age < 18 }">少年</c:when>
<c:otherwise>其他...</c:otherwise>
</c:choose>
<br><br>
<h3>jstl核心标签库,流程控制操作</h3>
<!-- 6.c:forEach -->
<!-- 1至10,每隔三个数输出 -->
<c:forEach begin="1" end="10" step="3" var="i" >
${i }、
</c:forEach>
<br><br>
<!-- 遍历集合数据 -->
<%
//List
List<Student> students = new ArrayList<Student>();
students.add(new Student(1,"AA"));
students.add(new Student(2,"BB"));
students.add(new Student(3,"CC"));
students.add(new Student(4,"DD"));
students.add(new Student(5,"EE"));
request.setAttribute("stu", students);
//Map
Map<String, Student> studentMap = new HashMap<String, Student>();
studentMap.put("a", new Student(1, "AAA"));
studentMap.put("b", new Student(2, "BBB"));
studentMap.put("c", new Student(3, "CCC"));
studentMap.put("d", new Student(4, "DDD"));
studentMap.put("e", new Student(5, "EEE"));
request.setAttribute("stuMap", studentMap);
%>
<c:forEach items="${requestScope.stu }" var="student" varStatus="status">
${student.id }、${student.name }、${status.index }、${status.count }、${status.first }、${status.last }、<br>
</c:forEach>
<br><br>
<c:forEach items="${requestScope.stu }" var="student" begin="1" step="2">
${student.id }、${student.name }<br>
</c:forEach>
<br><br>
<c:forEach items="${requestScope.stuMap }" var="student">
${student.key }、${student.value.id }、${student.value.name }<br>
</c:forEach>
<br><br>
<!-- 7.c:forToken -->
<c:set value="a,b,c,d.e.f.g.h" var="test" scope="request"></c:set>
<c:forTokens items="${requestScope.test }" delims="," var="str">
${str }<br>
</c:forTokens>
<br><br>
<h3>jstl核心标签库,URL操作</h3>
<!-- 8.c:import -->
<br><br>
<c:import url="http://www.baidu.com" charEncoding="UTF-8"></c:import>
<br><br>
<!-- 9.c:redirect,从当前页面重定向到指定页面 -->
<!-- 其中/代表的是web应用,所以该操作是服务器跳转,而Servlet中的重定向是客户端跳转 -->
<%--
<c:redirect url="test.jsp"></c:redirect>
<c:redirect url="/demo3_JSTL/test.jsp"></c:redirect>
--%>
<!-- 10.c:url,产生一个url地址 -->
<c:url value="/demo3_JSTL/test.jsp" var="varValue">
<c:param name="name" value="Test你好!"></c:param>
</c:url>
<br><br>
url:${varValue}
</body>
</html>
test.jsp
<body>
<h3>重定向到该页面!!</h3>
</body>
MyElFunction.java
package com.test.MyElFunction;
/**
* EL表达式自定义函数
*/
public class MyElFunction {
public static String concat(String str1, String str2){
return str1 + str2;
}
}
JSP(8)—EL案例和JSTL案例的更多相关文章
- JSP、EL表达式、JSTL标签库干货(建议收藏)
JSP(Java Server Pages)类似于ASP技术,它是在传统的网页HTML文件(.htm,.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件, ...
- 简述jsp之EL表达式和jstl及其使用
Jsp的指令之include指令include指令:代表的是页面的包含. 作用:可以把一些jsp的页面包含在一起,对外展示. 页面的布局,现在已经不用了,现在都用css+div进行布局.include ...
- JSP和El表达式和JSTL标签库使用
核心标签库: <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...
- JSP、EL表达式、JSTL
JSP 1.什么是jsp? Java Server Pages: java服务器端页面.可以理解为一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码.其本质就是一个Servlet. ...
- 个人整理的jsp、EL表达式、JSTL标签库的笔记,少概念多实用,需要的留下邮箱,会第一时间分享原稿PDF给大家!
jsp 第一章 jsp介绍及对比servlet 作用: 动态网页技术,动态的从数据库获取数据 jsp和servlet的优缺点: jsp优点:页面表现方便,利于写html代码 jsp缺点:业务逻辑处理麻 ...
- JAVA基础之JSP与EL技术、JSTL技术
要牢记jsp四大作用域(pageContext域:当前jsp页面范围 request域:一次请求 session域:一次会话 application域:整个web应用)以及九大内置对 ...
- EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统
EL&Filter&Listener-授课 1 EL表达式和JSTL 1.1 EL表达式 1.1.1 EL表达式介绍 *** EL(Expression Language):表达式语言 ...
- 零基础学习java------34---------登录案例,域,jsp(不太懂),查询商品列表案例(jstl标签)
一. 简单登录案例 流程图: 项目结构图 前端代码: <!DOCTYPE html> <html> <head> <meta charset="UT ...
- 第75节:Java的中的JSP,EL和JSTL
第75节:Java中的JSP,EL和JSTL 哭吧看不完的!!! Cookie和`Session 请求转发和重定向的区别: 地址不一样 请求次数也不一样 数据无法传递 4.跳转范围有限制 效率 请求转 ...
随机推荐
- mysql基本操作(一)
1.登录mysql mysql -h localhost -u root -p 登录mysql,其中 -h是指定要连接mysql服务器的主机名 -u是指定用户 -次数登录必须用-p输入密 ...
- ip访问网站和localhost访问网站中top使用
对于相对定位,使用margin-top不用简单使用top. top在localhost中能正常显示,在ip访问时会出现多余空白. margin-top不管是localhost中还是ip中都能正常显示.
- 【Android】Android EditText 去除边框
[Android]Android EditText 去除边框 将EditText属性设置修改 android:background="@null" //////////////// ...
- Codeforces 1017F The Neutral Zone 数论
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1017F.html 题目传送门 - CF1017F 题意 假设一个数 $x$ 分解质因数后得到结果 $x=p ...
- IDEA创建lo4j模板
复制文字到文本框中: log4j.rootLogger=DEBUG,stdout log4j.logger.org.mybatis=DEBUG log4j.appender.stdout=org.ap ...
- day21 模块 异常处理
常用模块:http://www.cnblogs.com/Eva-J/articles/7228075.html 今日概要: #time # —— 时间:时间戳 字符串 结构化时间 #collectio ...
- Java大数相乘-hdu1063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1063 题目描述: 代码实现: import java.util.Scanner; import jav ...
- hexo添加404公益界面
http://hellolb.top/2018/08/16/hexo添加404公益界面/ hexo个人博客添加404公益界面,这里我使用的腾讯404公益界面 我的博客主题是hexo+yilia,其实所 ...
- 从函数式编程到Ramda函数库(一)
函数式编程是种编程方式,它将电脑运算视为函数的计算.函数编程语言最重要的基础是λ演算(lambda calculus),而且λ演算的函数可以接受函数当作输入(参数)和输出(返回值).和指令式编程相比, ...
- MySQL 安装包下载教程
http://www.mysql.com/downloads/