JavaWeb Tomcat (七)JSP (下)EL表达式 JTSL标签库

动力节点JavaWeb视频
从底层源码开始讲起, 讲解的非常详细,对比过尚硅谷的视频,推荐动力节点。
视频是直播的录像,有些时候比较啰嗦,尤其到EL表达式之后,1小时的内容能讲3小时。

系列文章目录

EL表达式

  • 作用:
    简化JSP中代码,从四个域中取出数据,自动转换成字符串,并输出到浏览器。(替换<%= 表达式%>)
  • 表达式:${ 表达式 }
从不同的数据类型中取值。
  • 只能获取四个域中通过setAttribute方法设置的键值对
    注意:取值时key不需要" "
	request.setAttribute("key", "value");
	${ key }
  • 没有值返回空字符串
    如果这个键没有值,浏览器中什么都不会显示,不会在浏览器显示null字符。
	${ key1 }
  • 类对象的属性值
    对象的属性用.,可以使用三目运算符
<%
    Student zhangsan = new Student(1, "zhangsan", true);
    request.setAttribute("student", zhangsan);
%>

${ student.no }
${ student.name }
${ student.sex?"男":"女" }

获取属性值虽然是调用了对象的get方法,但是这个get方法是通过拼接字符串的方式拼接的。例如:${ student.no }中,no字符串首字母大写,拼接get字符串,成为getNo()方法执行。

  • 从Map集合的对象中取数据
<%
    Student zhangsan = new Student(1, "zhangsan", true);
    Student lisi = new Student(2, "李四", false);
    Map<String, Student> studentMap = new HashMap<>();
    
    studentMap.put("zhangsan", zhangsan);
    studentMap.put("lisi", lisi);
    request.setAttribute("studentMap", studentMap);
%>

	${ studentMap.zhangsan.no }
	${ studentMap.zhangsan.name }
	${ studentMap.zhangsan.sex }
  • 从数组中取数据
<%
    Student zhangsan = new Student(1, "zhangsan", true);
    Student lisi = new Student(2, "李四", false);

    List<Student> studentList = new ArrayList<>();
    studentList.add(zhangsan);
    studentList.add(lisi);
    request.setAttribute("studentList", studentList);  
%>
    ${ studentList[0].name }
	${ studentList[0].no }

总结: 对于有序的集合可通过下标[0]获取值,无需的集合通过.key值获取值

  • 忽略EL表达式
    • 忽略页面上所有的EL表达式:<%@ page isELIgnored="true|false" %> 默认为true,启动EL表达式
    • 忽略单行EL表达式:\。例如:\${ 表达式 }
在EL表达式中的隐含对象
  1. pageContext(JSP的九大内置对象,页面域)用于获取其他的内置对象。
    通过pageContext获取根目录:${ pageContext.request.contextPath };contextPath 拼接为:getContextPath()。
  2. paramparamValues 获取通过GET\POST提交的请求体数据。

数据格式:name=yudaye&aihao=chouyan&aihao=hejiu&aihao=tangtou
第一种方式:通过request请求域获取。

		<%  String name = request.getParameter("name");
		    String[] aihaos = request.getParameterValues("aihao");
		%>
		<%=name%>
		<%=aihaos[0]%>\<%=aihaos[1]%>\<%=aihaos[2]%>

第二种:通过EL表达式隐含对象获取

		${param.name}
		${paramValues.aihao[0]}、${paramValues.aihao[1]}、${paramValues.aihao[2]}
  1. 获取web.xml全局配置
		<web-app ...>
		<--- 全局配置 -->
		    <context-param>
		        <param-name>size</param-name>
		        <param-value>50</param-value>
		    </context-param>
		    <context-param>
		        <param-name>page</param-name>
		        <param-value>10</param-value>
		    </context-param>
		    .
		    .
		</web-app>

第一种:通过应用域application获得全局配置

		<%
		    String size = application.getInitParameter("size");
		%>
		<%=size%>

第二种:通过隐含对象initPatam获取全局配置

		${ initParam.size }
EL 表达式的运算符

提示: 逻辑部分应该放在Java 中处理,JSP中只做数据展示,尤其是EL表达式。

  1. 算术运算符:+ - * / %。在EL表达式中"+"只能做求和运算不能做字符串拼接
  2. 关系运算符:== 、!=、<、>、>=、<=、eq==eq是相同的都是调用equals方法判断。例:${ a eq b }
  3. 逻辑运算符:!、&&、||、not、and、or!not是相同的
  4. 条件运算符(三目运算符): ? :
  5. 取值运算符:[ ].
  6. 判空运算符:empty;为空返回true,不为空返回true

JSTL标签库

JSTL核心标签超详细
作用:简化JSP页面的Java代码。代替<% java 代码 %>
下载jar包,在lib文件夹中引入。

特别注意:出入参数如果是EL表达式,表达式与参数的引号之间不能有空格

在JSP文件中引入JSTL标签库

perfix参数:在使用时的名字,随便取。
uri参数:标签库的路径

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL标签库配置文件中各标签的含义
  • 全局配置:
<taglib ...>
  <description>JSTL 1.2 core library</description>		全局的描述信息
  <display-name>JSTL core</display-name>		名称,JSTL核心标签库
  <tlib-version>1.2</tlib-version>		版本
  <short-name>c</short-name>		缩写
  <uri>http://java.sun.com/jsp/jstl/core</uri>		定义的标签路径,在jsp中想要使用,就需要指定这个路径

  <validator>		验证
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV		
    </validator-class>
  </validator>
  <tag>标签的配置</tag>
  <tag>标签的配置</tag>
</taglib>
  • 标签的配置,if标签示例
<tag>
    <description>		功能描述
	Simple conditional tag, which evalutes its body if the
	supplied condition is true and optionally exposes a Boolean
	scripting variable representing the evaluation of this condition
    </description>
    <name>if</name>		名称
    <tag-class>org.apache.taglibs.standard.tag.rt.core.IfTag</tag-class>		标签的实现类
    <body-content>JSP</body-content>	标签体中是否支持JSP代码(包含EL)
    
    <attribute>			if标签的参数 test
        <description>		描述
The test condition that determines whether or
not the body content should be processed.
        </description>
        <name>test</name>					名称:test参数
        <required>true</required>			该参数是否必须传入
        <rtexprvalue>true</rtexprvalue>		参数的值是否支持JSP代码
	<type>boolean</type>					返回值类型boolean
    </attribute>	参数的标签结束
    
    <attribute>								if标签的参数 var
        <description>						描述
Name of the exported scoped variable for the		if判断条件的值为真,var返回true,否则返回false
resulting value of the test condition. The type
of the scoped variable is Boolean.        
        </description>
        <name>var</name>					名称:var。
        <required>false</required>			该参数不是必须的
        <rtexprvalue>false</rtexprvalue>	参数的值不能使用JSP代码
    </attribute>		var参数结束
    <attribute>
        <description>
				Scope for var.
        </description>
        <name>scope</name>				if标签中使用哪个域的值,只要名称不同,不用指定域
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
常用标签的使用
  1. if标签
    test参数中为判断条件,支持JSP语言,var参数返回表达式的值:true|false
		<c:if test="${param.name == 'zhangsan'}"  var="v" >
		    <h2>${v}</h2>
		</c:if>

浏览器输入:http://localhost:8080/jsp/3.jsp?name=zhangsan
输入:true
2. foreach标签
注意:items="${ requestScope.studentList } "这个代码中,引号和括号之间不能有空格。

<%
    ArrayList<Student> list = new ArrayList<>();
    Student zhangsan = new Student(1, "zhangsan", true);
    Student lisi = new Student(2, "李四", false);
    Student wangwu = new Student(3, "王五", true);
    list.add(zhangsan);
    list.add(lisi);
    list.add(wangwu);
    request.setAttribute("studentList", list);
%>
<c:forEach items="${ requestScope.studentList }" var="ele">
    no:${ele.no}</br>
    name:${ ele.name }</br>
    sex:${ele.sex ? "男" :"女"}</br>
</c:forEach>
  1. choose,when,otherwise标签(类似swith)
    choose标签只是作为父标签使用
<c:choose>
    <c:when test="${param.score > 90}">
        <h2>优秀</h2>
    </c:when>
    <c:when test="${param.score > 60}">
        <h2>及格</h2>
    </c:when>
    <c:otherwise>
        <h2>不及格</h2>
    </c:otherwise>
</c:choose>

Session 会话域

  • 作用:服务器识别用户并保持用户信息在一次访问中的连续性。一次:session失效
requestsessionapplication
名称请求域会话域应用域
对应的类名HttpServletRequestHttpSessionServletContext
生命周期一次请求开始到结束浏览器第一次访问服务器到Session超时或者用户关闭浏览器服务器调用Sevlet到服务器关闭
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值