熟悉又陌生的JavaWeb 第4天 JSTL表达式

传送门

熟悉又陌生的JavaWeb 第0天
熟悉又陌生的JavaWeb 第1天 项目搭建
熟悉又陌生的JavaWeb 第2天 基本JSP语法
熟悉又陌生的JavaWeb 第3天 Servlet编程
熟悉又陌生的JavaWeb 第4天 JSTL表达式
熟悉又陌生的JavaWeb 第5天 常用框架与Web安全

JSP和JavaBean

认识JavaBean

在JSP中使用JavaBean

为了好看,jsp里只有xml标签,也就是后来的jspx后缀文件的基础
很不易读,不建议这么写

<jsp:useBean id="tagNewObj" class="com.lazyking.Student" />
<jsp:setProperty name="tagNewObj" property="id" param="no" />
<jsp:setProperty name="tagNewObj" property="name" value="tag小明" />
//相同作用
    Student tagNewObj = new Student();
    tagNewObj.setId(Integer.parseInt(request.getParameter("no")));
    tagNewObj.setName("tag小明");

JavaBean的范围

page request session application
没什么特殊的,与xx.setAttribute是同样的作用

DAO和VO

课后习题

EL和JSTL

关系:EL -> JSTL,JSTL是强化版本
表达式解析相关的源代码可以看el包下的内容

EL与JSTL解析

一定要添加到作用域才可以使用表达式,否则取不到想要的数据
取值的逻辑源码里写了 ImplicitObjectELResolver

 ImplicitObjectELResolver_getValue

认识表达式语言

基本运算符

有一些等效的写法

  • div \
  • mod %
  • eq ==
  • ne !=
  • lt <
  • gt >
  • le <=
  • ge >=
  • and &&
  • or ||
  • not !

数据访问

<%
    request.setAttribute("requestSetStudent",new Student(1,"requestStudent"));
    pageContext.setAttribute("pageSetStudent",new Student(2,"pageStudent"));
    session.setAttribute("sessionSetStudent",new Student(3,"sessionStudent"));
    application.setAttribute("applicationSetStudent",new Student(4,"applicationStudent"));
%>
<ul>
    <li>${requestScope.requestSetStudent.name}</li>
    <%-- 不加前缀也能用 会按顺序找--%>
    <li>${requestSetStudent.name}</li>
    <li>${pageScope.pageSetStudent.name}</li>
    <li>${sessionScope.sessionSetStudent.name}</li>
    <li>${applicationScope.applicationSetStudent.name}</li>
    <%-- [] . 等效写法 []支持特殊字符串和变量(少见) --%>
    <li>${applicationScope.applicationSetStudent["name"]}</li>
    <%-- 获取RequestParam --%>
    <li>${param.no}</li>
</ul>

认识JSTL

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

源代码
JSTL源代码

核心标签库

XML标签库简介

国际化标签库简介

数据库标签库简介

函数标签库简介

自定义标签

自己加的章节,我觉得很重要很实用

旧方式

继承jakarta.servlet.jsp.tagext.TagSupport
自定义标签

public class CustomTld extends TagSupport {

    @Override
    public int doStartTag() throws JspException {
        try {
            pageContext.getOut().print("Hello, Custom Tag!");
        } catch (IOException e) {
            throw new JspException("Error in MyCustomTag", e);
        }
        // 表示不处理标签体内容
        return Tag.SKIP_BODY; 
    }

}

在WEB-INF里新建.tld后缀文件并配置

<?xml version="1.0" encoding="UTF-8"?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>customTld</short-name>
    <uri>http://mycompany.com/customTld</uri>

    <!-- Invoke 'Generate' action to add tags or functions -->
    <tag>
        <name>greet</name>
        <tag-class>com.lazyking.CustomTld</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>


使用tld

<%@ taglib prefix="my" uri="http://mycompany.com/customTld"  %>
<my:greet/>

新方式

直接在WEB-INF里新建一个tag后缀的文件

<%--定义HelloWorld.tag--%>
<%@ tag description="Simple Hello Tag" pageEncoding="UTF-8" %>
Hello, World!

<%--使用--%>
<%@ taglib prefix="mytags" tagdir="/WEB-INF/tags" %>
<mytags:HelloWorld />

自定义标签解析后的源码
自定义标签解析后的源码

课后习题

posted @ 2024-03-11 17:58  迷路的哨兵甲  阅读(7)  评论(0编辑  收藏  举报