EL表达式

Expression Language
image.png
这个EL表达式挺神奇的有点类似于jQuery

  1. package cn.java.controller;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /**
  9. * Servlet implementation class ELServlet
  10. */
  11. @WebServlet("/ELServlet")
  12. public class ELServlet extends HttpServlet {
  13. private static final long serialVersionUID = 1L;
  14. /**
  15. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  16. */
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18. //1、首先获取username和age属性值
  19. String username = request.getParameter("username");
  20. String age = request.getParameter("age");
  21. //2、将获取的数据保存到request域中
  22. request.setAttribute("username", username);
  23. request.setAttribute("age", age);
  24. //3、跳转到2.jsp页面,通过EL表达式来取出request域中的数据
  25. request.getRequestDispatcher("2.jsp").forward(request, response);
  26. }
  27. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28. // TODO Auto-generated method stub
  29. doGet(request, response);
  30. }
  31. }

1.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="<%=request.getContextPath() %>/ELServlet" method="post">
  11. 用户名: <input type = "text" name = "username"><br>
  12. 年龄:<input type = "text" name ="age"><br>
  13. <input type="submit" value="提交">
  14. </form>
  15. </body>
  16. </html>

2.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>通过EL表达式取数据</title>
  8. </head>
  9. <body>
  10. 姓名:${username}<br>
  11. 年龄:${age }
  12. </body>
  13. </html>

JSTL

JavaServerPages Standard Tag Library
image.png

image.png

通用标签image.png

image.png
scope代表域

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <!-- set标签主要是王指定的域中存放数据 -->
  12. <c:set var = "user" value = "张三" scope = "request"></c:set>
  13. <!-- out标签将数据进行打印显示 -->
  14. <c:out value = "${user}" ></c:out>
  15. <hr>
  16. <c:remove var="user" scope="request"/>
  17. <hr>
  18. <c:out value = "${user}" ></c:out>
  19. </body>
  20. </html>

以上jsp的显示:
image.png

条件标签

都有个test,test里面写判断条件

if

choose

choose需要配合when和otherwise使用

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <c:set var="age" value="13" scope="request"></c:set>
  12. <!-- if标签
  13. test:接判断的条件,如果提交为true,执行标签体内的内容
  14. -->
  15. <c:if test="${age == 12 }">
  16. 您的年龄为12岁。
  17. </c:if>
  18. Hello World~
  19. <hr>
  20. <c:choose>
  21. <c:when test="${age == 12 }">
  22. 您的年龄为12岁。
  23. </c:when>
  24. <c:otherwise>
  25. 您的年龄不是12岁。
  26. </c:otherwise>
  27. </c:choose>
  28. </body>
  29. </html>

迭代标签

forEach