浅谈JSP中JSTL【标签库】常用标签,EL表达式在JSP四大域中取值:

版权声明:本站所提供的文章资讯、软件资源、素材源码等内容均为本作者提供、网友推荐、互联网整理而来(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考,如有侵犯您的版权,请联系我,本作者将在三个工作日内改正。 https://blog.csdn.net/weixin_42323802/article/details/82916081

声明:本测试使用的Tomcat9,JDK9 创建web4.0项目进行测试:


EL表达式中字符串【能转为数字的】会自动强转:

EL表达式的全称: Expression Language  ;作用:EL表达式是从 session 中取值,【取代了JSP的脚本表达式】,格式: ${EL表达式内容}  ;JSP的四大域:page ,request ,session ,application

//xxx.setAttribute(name,value)
//page域,数据仅当前jsp页面有效                  ${pageScope.xxx}
 //request域,请求                               ${requestScope.xxx}
 //session域,会话                               ${sessionScope.xxx}
 //application域,项目对应的ServletContext域      ${applicationScope.xxx}

JSTL【标签库】常用标签如下:

//1. prefix:是jstl标签在使用的时候的前缀;
//2. uri:是标签库的资源路径;
//<c:if>标签,if判断语句。<c:if test=判断条件></c:if>
//<c:foreach>标签,遍历list和map集合
          /*var:在不循环对象的时候,保存的是控制循环的变量;在循环对象的时候,保存的是被循环对象中的元素
            items:指定要循环的对象
            varStatus:保存了当前循环过程中的信息(循环的开始、结束、步长、次数等)
            begin:设置循环的开始
            end:设置循环的结束
            step:设置步长——间隔几次循环,执行一次循环体中的内容*/
//choose标签,通常和 when, otherwise一起使用,进行条件语句判断:

遍历list 集合,这里使用 jsp  直接书写【方便测试】:

<%@ page import="com.baidu.beans.Product" %>
<%@ page import="java.util.stream.Stream" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page import="java.util.List" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>list</title>
</head>
<body>
<%
    Product p1 = new Product(1.0,"瓜子");
    Product p2 = new Product(20,"面包");
    Product p3 = new Product(10,"矿泉水");
    Product p4 = new Product(5,"槟榔");
    Product p5 = new Product(25,"啤酒");
    List<Product> list = Stream.of(p1, p2, p3, p4, p5).collect(Collectors.toList());
    request.setAttribute("list", list);
%>
<%--对list 进行遍历--%>
<c:forEach items="${list}" var="products">
    <p>${products.price}</p>
    <p> ${products.name}</p>
</c:forEach>
</body>
</html>

实体类Product ,需要提供无参有参构造,tostring, getter: 【成员变量double price,String  name】

访问:


遍历map 集合:

<%@ page import="com.baidu.beans.Product" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page import="java.util.stream.Stream" %>
<%@ page import="java.util.Map" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>map</title>
</head>
<body>
<% Product p1 = new Product(1.0,"瓜子");
    Product p2 = new Product(20,"面包");
    Product p3 = new Product(10,"矿泉水");
    Product p4 = new Product(5,"槟榔");
    Product p5 = new Product(25,"啤酒");
    //  :: jdk8新特性,用于传递方法或者函数
    Map<Double, String> map = Stream.of(p1, p2, p3, p4, p5).collect(Collectors.toMap(Product::getPrice, Product::getName));
    request.setAttribute("map", map);%>
<%--对map集合 进行遍历--%>
<c:forEach items="${map}" var="items">
    <font color="aqua">key:</font> ${items.key}<font color="aqua">value:</font> ${items.value}<br>
</c:forEach>
</body>
</html>

运行Tomcat 访问  jsp 页面:  

 choose标签,通常和 when, otherwise一起使用,进行条件语句判断:

package com.baidu.test;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "SwitchServlet", urlPatterns = "/ss")
public class SwitchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int temp=7;
        switch (temp){
            case 1:
                sop("case1");
                break;
                case 2:
                sop("case2");
                break;
                case 3:
                sop("case3");
                break;
                case 4:
                sop("case4");
                break;
                case 5:
                sop("case5");
                break;
                case 6:
                sop("case6");
                break;
                case 7:
                sop("case7");
                break;
                default:
                sop("error");
                break;
        }
        request.setAttribute("temp",temp );
        request.getRequestDispatcher("/choose.jsp").forward(request,response );
    }
    private void sop(Object obj) {
        System.out.println(obj);
    }

}

jsp  页面, 其中使用  EL表达式  和  JSTL  标签;

//1. prefix:是jstl标签在使用的时候的前缀;
//2. uri:是标签库的资源路径;
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<c:choose>
    <c:when test="${temp==1}" >
        <c:out value="case1"></c:out>
    </c:when>
    <c:when test="${temp==2}" >
        <c:out value="case2"></c:out>
    </c:when>
    <c:when test="${temp==3}" >
        <c:out value="case3"></c:out>
    </c:when>
    <c:when test="${temp==4}" >
        <c:out value="case4"></c:out>
    </c:when>
    <c:when test="${temp==5}" >
        <c:out value="case5"></c:out>
    </c:when>
    <c:when test="${temp==6}" >
        <c:out value="case6"></c:out>
    </c:when>
    <c:when test="${temp==7}" >
        <c:out value="case7"></c:out>
    </c:when>
    <c:otherwise>
        <c:out value="error"></c:out>
    </c:otherwise>

</c:choose>
</body>
</html>

运行Tomcat 访问  jsp 页面: 

猜你喜欢

转载自blog.csdn.net/weixin_42323802/article/details/82916081
今日推荐