JSTL和EL表达式

EL表达式

表达式语言(Expression Language),或称EL表达式,简称EL,是Java中的一种特殊的通用编程语言,借鉴于JavaScript和XPath。主要作用是在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"%>




表单提交


    
用户名:
年龄:
  • 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"%>




提交后页面


        用户名:${username} 
年龄:${age}

JSTL

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

  • 需要导入此jar包
  • jsp页面导入<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

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"%>




Insert title here




    
    
    
    
    
    
    
    
    
    
    
        您的年龄为12岁
    
    
您的年龄为12岁 您的年龄不是12岁
  • 案例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 dataMap1 =  new HashMap<>();
        dataMap1.put("shopName", "联想笔记本");
        dataMap1.put("address", "北京");
        dataMap1.put("price", "4999");
        Map dataMap2 =  new HashMap<>();
        dataMap2.put("shopName", "神州笔记本");
        dataMap2.put("address", "南京");
        dataMap2.put("price", "3999");
        Map dataMap3 =  new HashMap<>();
        dataMap3.put("shopName", "小米笔记本");
        dataMap3.put("address", "深圳");
        dataMap3.put("price", "5999");
        
        List> 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"%>





通过JSTL+EL表达式迭代List集合



    
            
商品名称 产地 价格
${map.shopName } ${map.address } ${map.price}

你可能感兴趣的:(JSTL和EL表达式)