jsp中的路径、EL表达式、JSTL标签库

7.jsp页面中的路径
    一般情况下,jsp中路径问题是和我们之前在servlet中讨论的html里面的路径问题是一样的,但是在jsp中有一种情况是要特殊对待的。
    如果在jsp页面的上面写了这样一个脚本:
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    并且再<head>标签中加入了一个子标签:
    <base href="<%=basePath%>" />

    如果在jsp页面里面有上面说提到的俩个东西,那么在这个jsp页面中,我们再去写上一个相对路径(最左边没有加/的那种路径),它就不是相对于地址栏中的当前路径了,而是要相对于这个basePath变量所代表的这个路径.

    <base href="..">这个标签的作用:
    在没有这个标签的情况下,页面中相对路径的跳转,都要参照地址栏中的当前路径来跳转,但是页面中加上了这个<base>标签后,页面中的相对路径的跳转都要参照这个标签中所写的路径来跳转。
    注意:这里说的相对路径的跳转指的是最左边没有/的那种路径的跳转.

    8.EL表达式
    形式:${ }
    作用:从一个范围里面取值或者从一个对象中取值或是向页面输出值.
    1.接收客户端参数.
       ${param.name1 }
    2.指定范围并取值
       ${pageScope.name2 }
         ${requestScope.name3 }
         ${sessionScope.name4 }
         ${applicationScope.name5 }
        3.可以不指定范围再去取值
       ${name}
       这时候会按照pageContext request session application这样一个顺序依次的去找有没有一个叫name的值存在,一旦找到了就输出出来,最终没有找到那么就什么都不输出。
    
    4.取出一个对象中的属性值.
       ${requestScope.student.id}
       ${requestScope.student.name}
       ${requestScope.student.age}
       或者
       ${student.id}
       ${student.name}
       ${student.age}
       或者
       ${student["id"]}
       ${student["name"]}
       ${student["age"]}

       注意:比如 ${student.id}表示是要调用student对象中的getId方法,至于对象中有没有id属性对这个操作没有任何影响.

       如果Student类中一个方法是getAddress,返回一个Address类的对象,Address类中有一个方法getCity,这个时候我们就可以这样写去拿到city属性的值.
       ${student.address.city}
    

    5.输出字符串
        ${"hello"}

    6.输出运算结果或者boolean表达式
        ${1+1 }
        ${(1+2)*3-4+5*3 }
        ${1<3 }
        //为空的话返回true
        ${empty "" }
        ${empty "hello" }
        //取否 不为空的话返回true
        ${not empty "hello" }
        ${! empty "hello" }
        ${param.score >50 }
        ${param.score >60?"good":"bad" }
    
    7.输出数组、集合中的元素
        <%
        String[] str = {"hello","world"};
    
        List<String> list = new ArrayList<String>();
        list.add("zhangsan");
        list.add("lisi");
        
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("a",100);
        map.put("b",200);
        map.put("c",300);
        
        request.setAttribute("str",str);
        request.setAttribute("list",list);
        request.setAttribute("map",map);
        
        %>
    
        ${str[0] }<br>
        ${list[1] }<br>
        ${map["c"] }<br>

    9.JSTL标签库
    JSP Standard Tag Library(JSTL)
    1.让web项目支持JSTL标签库
    在myeclipse中,建一个web项目的时候,在对话框下面会有提示在当前项目是否需要加入JSTL标签库的支持.(J2EE5.0是默认会加入对JSTL的支持的)
    在eclipse中,建一个文本项目,默认都是不支持JSTL,所以需要我们自己把JSTL的jar包导入到项目中(复制粘贴到项目中的lib目录):jstl.jar  standard.jar

    2.把JSTL标签库导入到某一个jsp页面中
     使用jsp中的taglib指令:
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
     prefix="c"相当于给这个标签库起一个别名,将来在页面中就是用以c开头的标签来使用标签库中的标签。这个别名也可以叫其他的名字。
    
    <c:forEach>标签:
      遍历List集合:
      students是放进request对象中的一个List集合,集合中存放的是Student类型的对象.
      items=""属性值是要遍历的集合
      var=""  属性值是每次遍历到的对象用什么名字的变量去接收。
      <c:forEach items="${students}" var="stu">
        <tr>
            <td>${stu.id }</td>
            <td>${stu.name }</td>
            <td>${stu.age }</td>
        </tr>
     </c:forEach>
    
    遍历Map集合:
    map是一个Map类型的集合,放到了request对象中,entry是我们顶一个的一个变量,用做接收每次遍历到的集合中的一组键值对,我们可以通过entry.key entry.value分别拿到这次遍历到的key值和value值
    <c:forEach items="${map}" var="entry">
          ${entry.key }-->${entry.value.id } &nbsp; ${entry.value.name } &nbsp; ${entry.value.age }<br>
      </c:forEach>

    
    <c:out>标签:
    向页面输出内容
    <c:out value="hello"></c:out>
      <c:out value="${5+5}"></c:out>
    //students是放在request中的List集合,集合里面是Student对象
      <c:out value="${students[2].id}"></c:out>
    
    <c:set>标签:
    向某一个范围对象中存放一个值。
    <c:set var="name" value="zhangsan" scope="request"></c:set>

        <c:remove>标签:
    从某个范围对象中把某个值给移除掉.
    <c:remove var="name" scope="request"/>

    <c:if>标签:
    条件判断
    <%
      request.setAttribute("score",40);
      %>
          
      <c:if test="${score>85 }">
          <font color="red">你的分数超过了85分</font>
      </c:if>
    <c:if test="${score>95 }">
          <font color="red">你的分数超过了95分</font>
      </c:if>

    这样写相当于:
    if(score>85){
        ...
    }
    if(score>95){
        ...
    }


    <c:choose>标签
    <c:when>标签
    <c:otherwise>标签
    例如:
    <c:choose>
          <c:when test="${score>=90 }">优</c:when>
          <c:when test="${score>=80 }">良</c:when>
          <c:when test="${score>=70 }">中</c:when>
          <c:when test="${score>=60 }">及格</c:when>
          <c:otherwise>差</c:otherwise>
      </c:choose>
    相当于:
    if(score>=90){
    
    }else if(score>=80){
    
    }else if(score>=70){
    
    }eles if(score>=60){
    
    }else{
    
    }  

猜你喜欢

转载自blog.csdn.net/bifuguo/article/details/82965065