【Head First Servlets and JSP】笔记 25:JSTL 参考

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<%-- errorPage要优先于DD配置 --%>
<%@ page errorPage="errorPage.jsp" %>

<html>
<head>
  <title>$Title$</title>
</head>
<body>
<%-- <c:forEach> --%>
<table>
  <c:forEach var="movie" items="${movieList}">
    <tr>
      <td>${movie}</td>
    </tr>
  </c:forEach>
</table>
<br>

<%-- <c:forEach> varStatus --%>
<table>
  <c:forEach var="movie" items="${movieList}" varStatus="movieLoopCount">
    <tr>
      <td>${movieLoopCount.count}</td>
      <td>${movie}</td>
    </tr>
  </c:forEach>
</table>
<br>

<%-- 嵌套<c:forEach> 一般用在嵌套的数据结构 --%>

<%-- <c:if> 这个页面一般人看不到--%>
<c:if test="${param.userType eq 'member'}">
  <jsp:include page="input.jsp"/>
</c:if>
<br>

<%-- <c:choose> --%>
<c:choose>
  <c:when test="${param.userType == 'visitor'}">
    <p>Hello, visitor</p>
  </c:when>

  <c:when test="${param.userType == 'member'}">
    <p>Good evening.</p>
  </c:when>

  <c:otherwise>
    <p>unbelievable</p>
  </c:otherwise>
</c:choose>
<br>

<%-- <c:set> 规则和jsp标准动作一样,没有就创建新的 --%>
<c:set var="userLevel" scope="session" value="Cowboy" />
<p>${userLevel}</p>
<c:set var="Fido" value="${person.dog}" />

<c:set var="userLevel_2">
  Cowboy, Cowgirl
</c:set>
<p>${userLevel_2}</p>

<%-- <c:set>只能用来设置bean和map --%>
<c:set target="${dog}" property="name" value="huang" />
<p>${dog.name}</p>

<c:set target="${dog}" property="name">
  ${param.name}
</c:set>
<p>${dog.name}</p>

<%-- jsontag 尝试 --%>
<json:object>
  <json:property name="name" value="${dog.name}"/>
</json:object>

<%-- <c:remove> 删除变量 --%>
<c:remove var="userLevel" scope="session"/>
<%-- 不写作用域 或者书写正确的作用域 都可删除变量--%>
<p>userLevel = ${userLevel}</p>

<%-- <c:import><jsp:include><include file="...">更强大,可以包含容器之外的内容,原理与前者类似 --%>
<%--<c:import url="某个URL" />--%>
<%-- import简单的页面是可以的,复杂的页面会超时, 图片会乱码 --%>

<%-- c:param --%>
<c:import url="input.jsp">
  <c:param name="content" value="this is a param from index.jsp" />
</c:import>

<%-- jsp的URL重写 --%>
这是一个URL重写可用的超链接
<a href="<c:url value='/login.jsp' />"> click here </a>
<br>

<%-- url编码 --%>
<c:set var="last" value="Hidden Cursor" />
<c:set var="first" value="Crouching Pixels" />
<c:url value="/showMovies" var="inputURL">
  <c:param name="firstName" value="${first}" />
  <c:param name="lastName" value="${last}" />
</c:url>
<p>url using param is: ${inputURL}</p>
<p><a href="${inputURL}">URL_above</a></p>

<p>About to do a risky thing: </p>
<c:catch var="myException">
    <% int x = 10/0; %>
</c:catch>
<p>If you see this, we survived.</p>
<c:if test="${myException != null}">
    <p>There was an exception: ${myException.message}</p>
</c:if>

</body>
</html>

错误页面建议使用 web.xml :

<%@page isErrorPage="true" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <p>同志,请先登陆</p>
    <a href="login.jsp">click here</a>
    <hr>
    <p>${pageContext.exception}</p>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>showMovies</servlet-name>
        <servlet-class>showMovies</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>showMovies</servlet-name>
        <url-pattern>/showMovies</url-pattern>
    </servlet-mapping>

    <!-- 普遍型错误页面 -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/errorPage.jsp</location>
    </error-page>

    <!-- 具体异常型 -->
    <!-- http状态码型 -->
    <error-page>
        <error-code>404</error-code>
        <location>/errorPage.jsp</location>
    </error-page>

</web-app>

 说是留着参考。。事实上要用的时候还是直接谷歌、查文档实在。。。

posted @ 2017-08-04 16:47  xkfx  阅读(219)  评论(0编辑  收藏  举报