L'utilisation et les méthodes d'EL et JSTL

Un, le code

1. Code

package com.web;

import com.c3p0.pojo.User;

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;
import java.util.ArrayList;

@WebServlet("/as")
public class AServlet extends HttpServlet {
    
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        User user = new User("tom","123");
        User user2 = new User("tom2","222");
        User user3 = new User("tom","333");
        User[] arr={
    
    user,user2,user3};
        ArrayList<User> list = new ArrayList<>();
        list.add(user);
        list.add(user2);
        list.add(user3);
        //数组和集合,存进域中
        request.setAttribute("arr",arr);
        request.setAttribute("list",list);

        //转发到jsp
        request.getRequestDispatcher("/jstl/b.jsp").forward(request,response);
    }
}

<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/1/28
  Time: 9:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--定义用户类和猫类,用户中将猫作为成员变量--%>
<%!
   public class User {
    
    
       String name;
       int age;
       Cat cat;

       public String getName2() {
    
    
           return name;
       }

       public int getAge() {
    
    
           return age;
       }

       public Cat getCat() {
    
    
           return cat;
       }
   }
   public class Cat{
    
    
       String name;
       int age;

       public String getName() {
    
    
           return name;
       }

       public int getAge() {
    
    
           return age;
       }
   }
%>
<%
    //有2个人,每人养了一只猫.存进list集合,使用EL取出展示
    User user1 = new User();
    User user2 = new User();
    Cat cat1 = new Cat();
    Cat cat2 = new Cat();
    user1.cat=cat1;
    user2.cat=cat2;
    //张三  的猫的名字叫加肥 2岁
    user1.name="张三";
    cat1.name="加肥";
    cat1.age=2;
    //存进list集合
    ArrayList<User> users = new ArrayList<>();
    users.add(user1);
    users.add(user2);
    //集合存进域中
    pageContext.setAttribute("users",users);

%>
<%--获得的是get方法里面的值--%>
${
    
    pageScope.users[0].name2}的猫的名字叫
${
    
    pageScope.users[0].cat.name},${
    
    pageScope.users[0].cat.age}</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--
动态获取虚拟路径
--%>
${
    
    pageContext.request.contextPath}
<%--param就相当于 request.getParameter() 可以获取请求参数--%>
${
    
    param.age}
${
    
    param.sex}
<hr>
${
    
    cookie.JSESSION.name}------${
    
    cookie.JSESSION.value}<br>
${
    
    cookie['Webstorm-59a98a54'].name}---${
    
    cookie['Webstorm-59a98a54'].value}
<%--cookie可以获取每个cookie的值--%>
</body>
</html>

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/1/28
  Time: 10:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%!
    public class City{
    
    
        String name;
        int man;
        int money;

        public String getName() {
    
    
            return name;
        }

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

        public int getMan() {
    
    
            return man;
        }

        public void setMan(int man) {
    
    
            this.man = man;
        }

        public int getMoney() {
    
    
            return money;
        }

        public void setMoney(int money) {
    
    
            this.money = money;
        }
    }
%>
<%
    City city1 = new City();
    City city2 = new City();
    city1.setName("徐州");;
    city1.setMan(80);
    city1.setMoney(30);
    city2.setName("关羽");;
    city2.setMan(100);
    city2.setMoney(40);
    HashMap<String, City> map = new HashMap<String,City>();
    map.put("张飞",city1);
    map.put("关羽",city2);
    pageContext.setAttribute("map",map);

%>
<%--输出:张飞守得徐州城,80万人,粮草30万石,关羽守的荆州城,100万人,粮草40万石--%>
张飞守的${
    
    pageScope.map.张飞.name},有${
    
    pageScope.map.['张飞'].man}万人,粮草${
    
    pageScope.map.get("张飞").money}万石
<hr>
${
    
    "张飞守的".concat(pageScope.map.张飞.name).concat("城,有").concat(pageScope.map['张飞'].man).concat("万人,粮草")
.concat(pageScope.map.get("张飞").money).concat("万石")}



</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/1/28
  Time: 15:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--单个条件判断--%>
<c:if test="${not empty 'list'}">
    list不为空
</c:if>
<%--多个条件判断--%>
<%
    request.setAttribute("m",1);
%>
<c:set var="m" value="7" scope="request"/>
<hr>
<c:choose>
    <c:when test="${m>=1 && m<=3}">
        <h3>spring</h3>
    </c:when>
    <c:when test="${m>=4 && m<=6}">
        <h3>spring</h3>
    </c:when>
    <c:when test="${m>=7 && m<=9}">
        <h3>spring</h3>
    </c:when>
    <c:otherwise>
        <h3>winter</h3>
    </c:otherwise>
</c:choose>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/1/28
  Time: 15:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--输出5次hello--%>
<%--
var="i" i存在最小域pageContext中
--%>
<c:forEach begin="1" end="5" step="2" var="i" varStatus="vs">
    ${
    
    i}--${
    
    vs.index}--${
    
    vs.count} <br>
</c:forEach>
<hr>
<%--
增强for循环
for(int a:arr)
--%>
<%--增强for--%>
<c:forEach var="u" items="${list}">
    ${
    
    u.name}--${
    
    u.psw} <br>
</c:forEach>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table border="1" cellpadding="15" cellspacing="0">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>工资</th>
        <th>功能</th>
    </tr>
<c:forEach var="e" items="${list}">
    <tr>
        <td>${
    
    e.id}</td>
        <td>${
    
    e.name}</td>
        <td>${
    
    e.age}</td>
        <td>${
    
    e.money}</td>
        <td><input type="button" value="增加">
        <input type="button" value="删除">
        <input type="button" value="修改"></td>
    </tr>
</c:forEach>
</table>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%
    //request.getRequestDispatcher("/2.el.jsp?age=666").forward(request,response);
  %>
  ${
    
    pageContext.request.getRequestDispatcher("/2.el.jsp?age=888").forward(pageContext.request,pageContext.response)}
  <%--
  jsp动作
  --%>
  <jsp:forward page="/el/2.el.jsp?age=111">
    <jsp:param name="sex" value="aa"/>
  </jsp:forward>
  </body>
</html>

Pour résumer

Ce qui précède est la méthode et tout le code lié à el et jstl.

Je suppose que tu aimes

Origine blog.csdn.net/StruggleBamboo/article/details/114750914
conseillé
Classement