jstl学习笔记

来源:互联网 发布:粤语发音软件 编辑:程序博客网 时间:2024/05/22 17:21

1.index.jsp文件

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.Timestamp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%
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 'index.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>
 
 
  获得项目上下文的方式:pageContext.request.contextPath<br/>
  <c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
  该项目上下文为:${contextPath}<br/>
 
 
 
    This is my JSP page. <br>
   
    varStatus中对象的值有:<br/>
   
    current当前这次迭代的(集合中的)项
 index当前这次迭代从 0 开始的迭代索引
 count当前这次迭代从 1 开始的迭代计数
 first用来表明当前这轮迭代是否为第一次迭代的标志
 last用来表明当前这轮迭代是否为最后一次迭代的标志
 begin属性值
 end属性值
 step属性值 
   
    <br/>
    <c:forEach  begin="0" end="3" var="i" varStatus="stats">
       ${i} --${stats.current} ${stats.index} ${count} ${stats.first} ${stats.last} ${stats.begin} ${stats.end} ${stats.step}<br/>
    </c:forEach>
    <c:if test="${1==1}" var="bFlag">
    1=1?=  ${bFlag} <br/>
    </c:if>
   
    <%
    int num = 1;
    request.setAttribute("num",num);
     %>
 
    <c:choose>
      <c:when test="${num == 1 }">
        num==1 <br/>
      </c:when>
      <c:when test="${num == 2 }">
        num==2 <br/>
      </c:when>
      <c:otherwise>
        num is not equals!
      </c:otherwise>
    </c:choose>
    <br/> <br/>
     <%
    java.util.Date date = new java.util.Date();
    Timestamp myTimestamp = new Timestamp(date.getTime());
    request.setAttribute("myTimestamp",myTimestamp);
     %>
    将Timestamp转化成date <br/>
    <fmt:formatDate value="${myTimestamp}" pattern="yyyy-MM-dd HH:mm"/>  <br/> <br/> 
    或用
    <fmt:formatDate value="${myTimestamp}" type="both" dateStyle="default" timeStyle="default"/>
    <br/> <br/>
    
    解析的字符串应尽量放在Servlet后台中!
    <fmt:formatNumber value="133476.1591" pattern="#,#00.0#"></fmt:formatNumber><br/>
    <fmt:formatNumber value="133477890" type="currency"/><br/>
    <fmt:formatNumber value="13" type="currency" pattern="$.00"/> <br/>
    <fmt:formatNumber value="13" type="currency" pattern="$.0#"/><br/>
    <fmt:formatNumber value="13" type="percent" /> -- 1,300%  type 可以是currency、 number、 和percent。 <br/>
   
    <%List list = new ArrayList();
      list.add("aa");list.add("bb");list.add("cc");
      request.getSession().setAttribute("list",list);
     
      Map map = new HashMap();
      map.put("key1","aa");
      map.put("key2","bb");
      map.put("key3","cc");
      request.getSession().setAttribute("map",map);
     %>
    ${fn:length(sessionScope.list) }  --输出list的长度<br/>
    ${fn:length(sessionScope.map) }   --输出map的长度<br/>
   
    <c:forEach items="${map}" var="entry">
        <c:out value="${entry.key}" />=<c:out value="${entry.value}" />  <br/>
     </c:forEach>
     知道key值时,可用 map[key]进行输出
    即:<c:out value="${map['key1']}" />
   
    <br/>url链接处理<br/>
    <c:url value="/delUser.do" var="delUrl">
      <c:param name="userId" value="1"></c:param>
    </c:url>
    <a href="${delUrl}">删除用户</a>
  
   <br/>国际化 zh_CN或 en 例子basename为资源文件的位置:<br/>
   <%
   String strFmtLocale = "zh_CN"; //zh_CN
   request.setAttribute("strFmtLocale",strFmtLocale);
    %>
   <fmt:setLocale value="${strFmtLocale}"/>
   <fmt:setBundle basename="i18n/messages" var="msgBundle"/>
   <fmt:message bundle="${msgBundle}" key="errors.required">
     <fmt:param value="userName"></fmt:param>
   </fmt:message>
  
   另一种方式:
   <fmt:bundle basename="i18n/messages">
     <fmt:message key="errors.required">
       <fmt:param value="userPwd"></fmt:param>
     </fmt:message>
   </fmt:bundle>
  
   <!-- 引入文件 加上属性var时,好像就不能包含进来了.必须以/开头 -->
   <c:import url="/jstl/nap.jsp"></c:import>
 
  </body>
</html>

原创粉丝点击