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.跳转范围有限制 效率 请求转 ...
随机推荐
- 计算1至n中数字X出现的次数【math】
转自: nailperry 一.1的数目 编程之美上给出的规律: 1. 如果第i位(自右至左,从1开始标号)上的数字为0,则第i位可能出现1的次数由更高位决定(若没有高位,视高位为0),等于更高位数字 ...
- 详解 Java 中的三种代理模式
代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能. 这里使用 ...
- Azure附加新磁盘,差点掉进去的那个坑,注意临时数据盘
接今早的mysql问题,最终原因是mysql数据库的数据库文件以及pid丢失,当我还纳闷为什么丢失的情况下 我研究了下Azure云平台的数据磁盘原理,在Azure下,新建vm(centos)后只会提供 ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL
SelectNodes,SelectSingleNode总是返回NULL 原文地址:http://www.cnblogs.com/linlf03/archive/2011/11/30/2268705. ...
- 企业级代码托管Gitlab
Gitlab概述: 一个利用Ruby on Rails开发的开元应用程序,从而实现一个代码托管项目仓库,可以通过web界面进行访问公开的或者私有的项目 Ruby on Rails是一个可以使开发,部署 ...
- Python知识回顾 —— 面向对象
博客转载自 http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/linhaifeng/articles/62040 ...
- Mysql my.cnf配置文件记录
一.参数 1.max_binlog_size = 1G #binlog大小 2. #slave不需要同步数据库 binlog-ignore-db=information_schema bin ...
- 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
从第0行开始,输出第k行,传的参数为第几行,所以在方法中先将所传参数加1,然后将最后一行加入集合中返回. 代码如下: public static List<Integer> generat ...
- 019 python面相对象编程
一:self的意思 1.说明 self代表类的实例,而非类. 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self. self 代表的是类的实例 ...
- Chrome 浏览器数据无法同步,google账号登录失败,提示 Request canceled
解决方法: 进账号设置不同步 钱包数据 (即取消"Google Pay 中存储的付款方式和地址信息"项的同步) 参考链接: https://www.v2ex.com/t/45285 ...