zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Asp.Net Web API 2第一课——入门
    Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
    jsp 获取服务器ip 以及端口号
    对String值不可变的理解以及String类型的引用传递问题
    关于 SAXParseException Content is not allowed in Prolog (前言中不允许有内容)
    用tomcat插件 在Eclipse 中配置Tomcat项目
    docker保存日志文件到本地
    java split函数结尾空字符串被丢弃的问题
    byte类型的127+1=-128?
    java 中 Integer 传参方式的问题
  • 原文地址:https://www.cnblogs.com/wlxslsb/p/10743116.html
Copyright © 2011-2022 走看看