美文网首页
JSTL和EL表达式

JSTL和EL表达式

作者: 磊_5d71 | 来源:发表于2018-09-22 08:25 被阅读0次

    EL表达式

    表达式语言(Expression Language),或称EL表达式,简称EL,是Java中的一种特殊的通用编程语言,借鉴于JavaScriptXPath。主要作用是在Java Web应用程序嵌入到网页(如JSP)中,用以访问页面的上下文以及不同作用域中的对象 ,取得对象属性的值,或执行简单的运算或判断操作。EL在得到某个数据时,会自动进行数据类型的转换。

    表达式语法

    ${变量名}

    • 使用时需要导入外部包,在tomcat服务器上找到此包,并导入。
      apache-tomcat-8.5.34/lib/servlet-api.jar
    • 案例从1.jsp 页面提交,通过servlet跳转到showEL.jsp页面完成赋值的输出
    • 1.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>表单提交</title>
    </head>
    <body>
        <form action="<%=request.getContextPath() %>/ELServlet" method="post">
            用户名:<input type="text" name="username"><br>
            年龄: <input type="text" name="age"><br>
            <input type="submit" value="提交"> 
        </form>
    
        
    </body>
    </html>
    
    • ELServlet
    package com.alan.controller;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    @WebServlet("/ELServlet")
    public class ELServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1、首先获取username和age的属性值
            String username = request.getParameter("username");
            String age = request.getParameter("age");
            //2、将其保存到request域中
            request.setAttribute("username", username);
            request.setAttribute("age", age);
            //3、跳转到showEL.jsp页面,我们通过EL表达式取出request域中的值
            request.getRequestDispatcher("/showEL.jsp").forward(request, response);
            
    
        }
    
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
    
        }
    
    }
    
    • showEL.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>提交后页面</title>
    </head>
    <body>
            用户名:${username} <br>
            年龄:${age} 
    
    </body>
    </html>
    

    JSTL

    JSP标准标签库JSP Standard Tag Library)是Java EE网络应用程序开发平台的组成部分。它在JSP规范的基础上,扩充了一个JSP的标签库来完成一些通用任务,比如XML数据处理、条件执行、数据库访问、循环和国际化

    JSTL标签介绍

    • 通用标签 set out remove
    • 条件标签 if choose
    • 迭代标签 forEach
    • 案例1
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    
        <!-- set、out、remove标签 -->
        <!-- set标签主要往指定的域中存放数据 -->
        <c:set var="user" value="张三" scope="request"></c:set>
        <!-- 将数据打印输出 -->
        <c:out value="${user}"></c:out>
        <!-- remove标签 -->
        <c:remove var="user" scope="request" />
        <c:out value="${user}"></c:out>
        <!-- 
        if标签
            test:按判断的条件,如果条件为true,执行标签体中的内容
            
         -->
        <c:set var="age" value="12" scope="request"></c:set>
        <c:if test="${age==11}">
            您的年龄为12岁
        </c:if>
        <hr>
        <!-- choose标签 -->
        <c:set var="age1" value="13" scope="request"></c:set>
        <c:choose>
            <c:when test="${age1==12}">
                    您的年龄为12岁
            </c:when>
            <c:otherwise>
                    您的年龄不是12岁
            </c:otherwise>
        </c:choose>
    
    
    
    </body>
    </html>
    
    • 案例2 foreach
    • servelet
    package com.alan.controller;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    
    
    @WebServlet("/JSTLELServlet")
    public class JSTLELServlet extends HttpServlet {
    
    
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //1、将输入存入List中
            Map<String,Object> dataMap1 =  new HashMap<>();
            dataMap1.put("shopName", "联想笔记本");
            dataMap1.put("address", "北京");
            dataMap1.put("price", "4999");
            Map<String,Object> dataMap2 =  new HashMap<>();
            dataMap2.put("shopName", "神州笔记本");
            dataMap2.put("address", "南京");
            dataMap2.put("price", "3999");
            Map<String,Object> dataMap3 =  new HashMap<>();
            dataMap3.put("shopName", "小米笔记本");
            dataMap3.put("address", "深圳");
            dataMap3.put("price", "5999");
            
            List<Map<String,Object>> dataList = new ArrayList<>();
            dataList.add(dataMap1);
            dataList.add(dataMap2);
            dataList.add(dataMap3);
            
            //2、将List赋值到request的作用域中
            request.setAttribute("list", dataList);
            //3、在jsp页面取出List
            request.getRequestDispatcher("/2.jsp").forward(request, response);
        
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    
    • jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>通过JSTL+EL表达式迭代List集合</title>
    </head>
    <body>
    
        <table border="1" cellspacing="0" >
            <tr>
                <td>商品名称</td>
                <td>产地</td>
                <td>价格</td>
            </tr>
            <c:forEach items="${list}" var="map">
                <tr>
                    <td>${map.shopName }</td>
                    <td>${map.address }</td>
                    <td>${map.price}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:JSTL和EL表达式

          本文链接:https://www.haomeiwen.com/subject/izmznftx.html