el表达式和jstl

1.el表达式相当于jsp的简洁版:
jsp: request.getparameter(“xxx”)获取参数
el表达式:${“requestScope.xxx”}当然requestscope可以省略不写,
除了 request作用域 session application page都可以这样写 这样子就不用写太多的jsp的脚本
下图示例:

jar包在这里 -----》链接:https://pan.baidu.com/s/12dOrfvm_LoTsv9gOtp8dhA
提取码:fav6
如果是eclipse,你就点击右键 add to path 然后就添加到你的path中了
如果是idea,那么你就用右键 add as library中就行了

下面是el表达式取值

package entity;

public class User {
    private String name;
    private String password;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
package wu;

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

@WebServlet("/lb")
public class ListBean extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user1=new User("周杰伦","111");
        User user2=new User("刘德华","111");
        User user3=new User("周星驰","111");
        User user4=new User("华晨宇","111");
        List<User> list=new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        request.getSession().setAttribute("listbean",list);
        request.getRequestDispatcher("first/ListBean.jsp").forward(request,response);

    }
}
<%--
  Created by IntelliJ IDEA.
  User: name
  Date: 2020/2/23
  Time: 14:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ListBeanTest</title>
</head>
<body>
    ${listbean}
    </hr>
    ${listbean[0]}<br>
    ${listbean[1]}<br>
    ${listbean[2]}<br>
    ${listbean[3]}<br>
    <hr/>
    <table style="border: 1px green solid">
        <tr style="border: 2px red solid">
            <td > ${listbean[0].name}</td>
            <td> ${listbean[0].password}</td>
        </tr>
        <tr style="border: 1px red solid">
            <td> ${listbean[1].name}</td>
            <td> ${listbean[1].password}</td>
        </tr>
        <tr style="border: 1px red solid">
            <td> ${listbean[2].name}</td>
            <td> ${listbean[2].password}</td>
        </tr>
        <tr style="border: 1px red solid">
            <td> ${listbean[3].name}</td>
            <td> ${listbean[3].password}</td>
        </tr>
    </table>

</body>
</html>

jstl如何for循环和if判断

package wu;

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("/if")
public class Jstl 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 {
        String num= request.getParameter("num");
        request.setAttribute("num",num);
        request.getRequestDispatcher("first/if.jsp").forward(request,response);
    }
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: name
  Date: 2020/2/23
  Time: 15:10
  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>
      <c:if test="${num%2==0}">// 这个test里面就是用el表达式取值进行判断如果是就输出蔬菜
          <font>蔬菜</font>
      </c:if>
      <c:if test="${num%2!=0}">
          <font>raw feat</font>
      </c:if>
</body>
</html>
普通for循环
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: name
  Date: 2020/2/23
  Time: 15:38
  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>	
//var 相当于 javafor循环中的 变量i begin:从什么位置开始 end:到那个位置结束 step:每一次 加多少 减多少


        <c:forEach var="i"  begin="0" end="${num}" step="1">
            ${i}
        </c:forEach>
</body>
</html>
 当然还有foreach 循环
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: name
  Date: 2020/2/23
  Time: 16:05
  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>
<c:forEach var="u" items="${listbean}">
<%--  ${u.name}----${u.password}<br>--%>
<%--        <ur/>--%>
    ${u}
</c:forEach>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值