• 7-EL表达式和JSTL表达式


    引入jar包

    一。EL表达式
    1.表达式语言,用于jsp网页中获取和计算数据
    2.语法:${表达式}
    3.用于取值:可以从pageContext,request,session,application这些域中获取后台数据。顺序是从小到大
    4.指定作用域来取值:${requestScope.对象名},${sessionScope.对象名},${applicationScope.对象名}
    5.EL表达式的开关:<%@ page isELIgnored="false" %> fasle:EL表达式有效 true:EL表达式无效
    6.EL表达式的判断:
    比较大小:> < ==
    是否为空:${empty 对象名} ${not empty 对象名}

    二。JSTL:jsp的标准标签库
    使用步骤:
    1.在项目中导入jar文件:jstl-1.2.jar,standard-1.1.2.jar
    2.在页面需要通过指令引入标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    3.使用标签:
    常用标签
    <c:if test="${判断条件}">
    <c:forEach items="${集合名}" var="迭代对象别名">内容由EL表达式获取</c:forEach>
    <C:redirect src="url">重定向
    <c:out value="${}">页面输出
    <c:set var="变量名" value="${值}" scope="作用域">
    <c:remove var="变量名" scope="作用域">

    4.fmt标签:是用作格式化输出的标签
    引入:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <fmt:formatDate value="${ci.beginDate }" pattern="yyyy-MM-dd"/>

    例:用el表达式替换jsp页面中的java代码

    <%@page import="com.pojo.Student"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <base href="<%=basePath%>"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <style type="text/css">
    </style>
    </head>
    <script type="text/javascript">
        function del(id){
            if(confirm("是否确定要删除该数据?")){
                window.location.href="updateStu?type=del&id="+id;
            }
        }
    </script>
    
    <c:set var="username" value="${stu.stuName}" scope="session"></c:set>
    <c:remove var="username" scope="session"></c:remove>
    
    <body>
    <div align="center">
        <table style=" 500px;" border="1">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>生日</th>
            <th>专业</th>
            <th>操作</th>
        </tr>
            <c:forEach items="${list}" var="stu">
            <tr>
                <td>${stu.stuId}</td>
                <td>${stu.stuName }</td>
                <td>${stu.stuAge}</td>
                <td>
                    <c:if test="${stu.stuSex=='1' }"></c:if>
                    <c:if test="${stu.stuSex=='2' }"></c:if>
                </td>
                <td><fmt:formatDate value="${stu.stuDate }" pattern="yyyy-MM-dd"/></td>
                <td>${stu.showStuProfess}</td>
                <td><a href="javascript:del('${stu.stuId }')">删除</a> 
                <a href="updateStu?type=toupdate&id=${stu.stuId }">修改</a></td>
            </tr>
            </c:forEach>
        </table>
    </div>
    </body>
    </html>
  • 相关阅读:
    servlet的提交
    servlet的doPost 和doGet和web文件结构
    helloServlet
    捕鱼达人
    The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt
    ACM-ICPC 2017 Asia Urumqi A. Coins
    Nordic Collegiate Programming Contest 2015​ B. Bell Ringing
    变量
    hiho 1050 树的直径
    ACM-ICPC 2017 Asia Urumqi G. The Mountain
  • 原文地址:https://www.cnblogs.com/wlxslsb/p/10743116.html
Copyright © 2020-2023  润新知