zoukankan      html  css  js  c++  java
  • el和jstl

    <%@page import="cn.bdqn.bean.User"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'el.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    <%--
    
    el表达式:  expression  language! 目的:简化我们之前书写的表达式  
    语法结构: ${表达式} 
    着重点:在于数据的显示!
    
    page        pageScope
    request     requestScope
    session     sessionScope
    application applicationScope
    
    比如说:我们把一个Student对象放入了session作用域中!  session.setAttribute("stu",student)
    我们怎么取值?   
    之前  session.getAttribute("stu");  
    现在 ${sessionScope.stu}||  ${stu}
    
    想使用el从作用域中取值!两种方式:
    01.${作用域.属性名}  从指定的作用域中查询指定的属性
    02.${属性名}  没有指定作用域!这时候,el表达式会按照顺序在作用域中查找!
     page-->request--->session--->application
    
    有个疑问??
    session.setAttribute("stu",student);
    requestScope.stu?????能不能拿到值!
    反之,我在request作用域中保存一个对象!在sessionScope中能拿到吗????!!!  不会!!!!
    作用域之间是相互独立的!
    
    el表达式的数据来源于====》四个作用域!
    
    --%>
      </head>
      <body>
     <%
     //向Scope中存放字符串
     pageContext.setAttribute("pageName", "page小白");
     request.setAttribute("requestName", "request小白");
     session.setAttribute("sessionName", "session小白");
     application.setAttribute("applicationName", "application小白");
     //向Scope中存放对象
     User user=new User();
     user.setId(5);
     user.setUserName("对象userName");
     user.setPassword("对象password");
     session.setAttribute("user", user);
     //向Scope中存放集合
     ArrayList<User> users =new ArrayList();
     users.add(user);
     session.setAttribute("userList", users);
     %>
     pageScope.pageName====>${pageScope.pageName}<br/>
     pageName====>${pageName}<br/>
     requestScope.pageName====>${requestScope.requestName}<br/>
     sessionScope.pageName====>${sessionScope.sessionName}<br/>
     applicationScope.pageName====>${applicationScope.applicationName}<br/>
     <hr/>
    <%-- 作用域.属性(对象).方法 --%>
    <%-- 作用域.属性(对象).属性 --%>
     用户名:${sessionScope.user.getUserName()}<br/>  
     用户名:${sessionScope.user.userName}<br/>
     用户名:${user.userName}<br/>
     用户名:${sessionScope.user["userName"]}<br/>
      <hr/>
    获取集合中对象的属性:${sessionScope.userList[0].password }
     
      </body>
    </html>
    之后使用el表达式修改新闻详情界面的数据!
    <%@page import="cn.bdqn.bean.User"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%--引入jstl标签库 --%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'jstl.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        jstl:(jsp standard tag library):是一个不断完善的jsp标签库!
         想使用:必须引入标签库!
        -->
      </head>
      <body>
    <%--
    value:需要输出的值,一般和el表达式连用
    escapeXml:是否转义特殊字符!默认true
    default:当value值为null的时候显示的值!
    
      --%>
      <c:out value="大家好" />
      <c:out value="&lt 大家好  &gt"  escapeXml="true"/>
      <c:out value="&lt 大家好  &gt"  escapeXml="false"/>
      <c:out value="${null}"  default="默认值sssss"/>  
      <hr/>
     <%
       User  user=new User();
     user.setUserName("小白");
     user.setPassword("admin");
     session.setAttribute("user", user);
     %> 
    <%--
    target:需要赋值的目标对象
    property:对象的属性
    value:属性值
    scope:对应的作用域
    var:我们声明的变量
     --%>
      <c:set  target="${sessionScope.user}"  property="email"  value="501804292@qq.com"/>
      <c:out value="${sessionScope.user.email}" />
    <%-- 声明了一个name变量,值为 小黑,放在了session作用域中 --%>
      <c:set var="name" value="小黑" scope="session"/>
      <c:out value="${sessionScope.name}" />
    <%--
    var:需要删除的变量名
    scope:从哪个作用域中删除
    --%>
      <c:remove var="name" scope="request"/>
      <c:out value="${sessionScope.name}" />
    <hr/>
    <%--
    c:choose:相当于我们java中的switch选择结构!
    c:when:相当于我们的case
    c:otherwise:相当于default
    --%>
    <c:choose>
      <c:when test="${name=='小黑'}">
           <div>姓名是:小黑</div>
      </c:when>
      <c:when test="${name=='小黑1'}">
           <div>姓名是:小黑1</div>
      </c:when>
      <c:otherwise>
            <div>姓名是:null</div>
      </c:otherwise>
    </c:choose>
    <hr/>
    <%
       List<User> users=new ArrayList();
    users.add(new User("小黑1","admin1"));
    users.add(new User("小黑2","admin2"));
    users.add(new User("小黑3","admin3"));
    users.add(new User("小黑4","admin4"));
    session.setAttribute("users", users);
    %>
    <%--c:forEach
    items: 需要循环的变量(集合)
    begin:开始的元素  下标从0开始
    end:最后一个元素  下标从0开始
    step:每一次迭代的步长
    var: 变量名称
    varStatus:代表循环状态的变量名称
    --%>
    <c:forEach items="${sessionScope.users}" var="user" >
       ${user.userName}==============${user.password }<br/>
    </c:forEach>
    
    <%--通过指定的分隔符delims  将 字符串 分割成一个 数组  !
    c:forTokens  也具有遍历的功能
     --%>
    <c:forTokens items="4,5,6" delims="," var="s">
       ${s}
    </c:forTokens>
    
    <%--
      引入需要的外部文件,  和 jsp:includ类似
      charEncoding:默认值  iso-8859-1
      var:引入界面的变量
      url:所引入的外部文件
    --%>
    <%--<c:import url="http://www.baidu.com" charEncoding="utf-8"/>--%>
    <c:import url="temp.jsp" var="data"/>
     <c:out value="${data}"/>
    
    <%--
    var:变量名
    value:跳转的路径!如果文件不在webRoot下,必须加上所在的文件夹名称
    c:url:一般和a标签连用,可以携带参数!
    --%>
    <c:url var="path" value="jstl/temp.jsp">
       <c:param name="jstlName" value="小白"/>
    </c:url>
    
    <a href="${path}">跳转到temp.jsp</a>
    
    
      
      </body>
    </html>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'temp.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        
        <h1>这是temp.jsp</h1>
         传递过来的参数是:${param.jstlName}
        
      </body>
    </html>
  • 相关阅读:
    Admin添加字段
    django admin基础
    user_admin
    admin 模块功能
    todolist项目
    Django进阶项目
    Django进阶
    bolg项目
    EL&jstl
    模拟用户登录,内含验证码验证和request等操作
  • 原文地址:https://www.cnblogs.com/xtdxs/p/7094702.html
Copyright © 2011-2022 走看看