JSTL与EL表达式笔记

2020-05-05  本文已影响0人  憨憨二师兄

EL表达式应用

初始EL表达式

EL表达式

示例程序:

Student

public class Student {
    private String name;
    private String tel;

    public Student(){

    }

    public Student(String name,String tel){
        this.name = name;
        this.tel = tel;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

StudentServlet

@WebServlet("/info")
public class StudentServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student stu = new Student("张三","123456");
        request.setAttribute("stu",stu);
        request.getRequestDispatcher("/info.jsp").forward(request,response);
    }
}

info.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>学生姓名:${requestScope.stu.getName()}</h1>
    <h1>学生手机:${requestScope.stu.getTel()}</h1>
</body>
</html>

在浏览器地址栏中输入:http://localhost:8080/el/info
页面显示结果:

EL的作用域对象

EL表达式内置四种作用域对象

如果忽略书写作用域对象时,el则会按照作用域从小到大依次尝试获取

示例程序:
StudentServlet

@WebServlet("/info")
public class StudentServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student stu = new Student("张三", "123456");


        HttpSession session = request.getSession();
        session.setAttribute("stu", stu);

        ServletContext context = request.getServletContext();

        request.setAttribute("grade", "A");
        session.setAttribute("grade", "B");
        context.setAttribute("grade", "C");


        request.getRequestDispatcher("/info.jsp").forward(request, response);
    }
}

info.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>学生姓名:${sessionScope.stu.getName()}</h1>
<h1>学生手机:${sessionScope.stu.getTel()}</h1>
<!-- 会按照从小到大的顺序进行匹配,所以最先匹配到的作用域为 request 结果输出为 A -->
<h1>成绩评级:${grade}</h1>
</body>
</html>

页面显示结果为:


EL表达式输出

EL输出参数值

示例:

info.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>学生姓名:${sessionScope.stu.getName()}</h1>
<h1>学生手机:${sessionScope.stu.getTel()}</h1>
<!-- 会按照从小到大的顺序进行匹配,所以最先匹配到的作用域为 request 结果输出为 A -->
<h1>成绩评级:${grade}</h1>
<h1>指导教师:${param.teacher}</h1>
</body>
</html>

页面显示结果:


JSTL应用

JSTL介绍

JSTL v1.2.5 组件介绍



JSTL有两种安装方式

JSTL的标签库种类
JSTL按照功能可以划分为五类标签库

引用JSTL核心库

JSTL判断标签

示例程序:
JSTL_if_Servlet

@WebServlet("/if_test")
public class JSTL_if_Servlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("score",87);
        request.setAttribute("grade","B");
        request.getRequestDispatcher("/core.jsp").forward(request,response);
    }
}

core.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${requestScope.score}</h1>
<c:if test="${requestScope.score >= 60}">
    <h1 style="color: green;">pass</h1>
</c:if>
<c:if test="${requestScope.score < 60}">
    <h1 style="color: red;">fail</h1>
</c:if>

<h1>${requestScope.grade}</h1>
<c:choose>
    <c:when test="${requestScope.grade == 'A'}">
        <h1>优秀</h1>
    </c:when>
    <c:when test="${requestScope.grade == 'B'}">
        <h1>良好</h1>
    </c:when>
    <c:when test="${requestScope.grade == 'C'}">
        <h1>及格</h1>
    </c:when>
    <c:otherwise>
        <h1>不及格</h1>
    </c:otherwise>
</c:choose>
</body>
</html>

页面显示结果为:


JSTL遍历集合

示例程序如下:
Student

public class Student {
    private String name;
    private int age;

    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

JSTL_forEach_Servlet

public class JSTL_forEach_Servlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三",18));
        list.add(new Student("阿珍",17));
        list.add(new Student("阿强",17));
        request.setAttribute("students",list);

        request.getRequestDispatcher("/core.jsp").forward(request,response);
    }
}

core.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:forEach items="${requestScope.students}" var="student" varStatus="i">
        <h1>${i.index + 1}. ${student.getName()}-${student.getAge()} </h1>
    </c:forEach>
</body>
</html>

页面显示如下:


fmt格式化标签库-日期

pattern中的格式:

yyyy - 表示四位年
MM   - 表示月
dd   - 表示日
HH   - 表示小时
mm   - 表示分
ss   - 表示秒
SSS  - 表示毫秒

示例程序如下:
fmt.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setAttribute("now", new java.util.Date());
%>

<h2>${requestScope.now}</h2>
<h2>
    <fmt:formatDate value="${requestScope.now}" pattern="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 SSS 毫秒" />
</h2>
</body>
</html>

页面显示:


fmt格式化标签库-数字

示例程序如下:
fmt.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setAttribute("num", 1234567.456);
%>

<h2>${requestScope.num}</h2>
<h2>
    <fmt:formatNumber value="${requestScope.num}" pattern="0.00" />
</h2>
<h2>
    ¥<fmt:formatNumber value="${requestScope.num}" pattern="0,000.00" />元
</h2>
</body>
</html>

页面显示如下:


上一篇 下一篇

猜你喜欢

热点阅读