JSP标签和JSTL

Java的5个标签库:核心(c)、格式化(fmt)、函数(fn)、SQL(sql)、XML(x)

  SQL、XML库不推荐使用

核心标签库(c)

//taglib指令
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

   <c:out>

//<c:out>标签用于在JSP中输出内容
//有value、default、escapeXml三个属性
//escapeXml用于对保留xml字符(<、>、'、"、、&)转义,设为false禁止转义
<c:out value="${someValue}" default="${defaultValue}"/>
//default可以写在标签内,用于设置默认值
<c:out value="${someValue}">${defaultValue}</c:out>

  <c:if>

//<c:if>用于控制是否渲染标签内的内容
//有test、var、scope三个属性
//test值为假,标签中的所有内容都会被忽略
<c:if test="false">
    这的内容都会被忽略
</c:if>

  <c:choose>、<c:when>、<c:otherwise>

//<c:choose>不包含属性,只作为<c:when>、<c:otherwise>的父标签
//<c:choose>中至少包含一个<c:when>,至多包含一个<c:otherwise>,并且只能是最后一个标签
<c:choose>
//<c:when>只包含一个test属性
    <c:when test="someCondition">if</c:when>
    <c:when test="someCondition">else if</c:when>
    <c:otherwise>else</c:otherwise>
</c:choose>

  <c:forEach>、<c:forTokens>

//<c:forEach>类似于java中的for循环
//有items、begin、end、step、var、varStatus六个属性
//java中的for循环
for(int i=0;i<100;i++)
    out.printfln(i);
//用<c:forEach>表示
//step默认值是1,可以不写
<c:forEach var="i" begain="0" end="100" step="1">i</c:forEach>
//items表示要遍历的集合、数组、Map、Iterator、Enumeration
//varStatus表示集合里的变量
<c:forEach items="lists" varStatus="student">
    ${student.name}
    ${student.number}
</c:forEach>
//<c:forTokens>的items是字符串
//<c:forTokens>标签与<c:forEach>标签有相似的属性,只多一个delims属性,delims是分隔符
<c:forTokens items="google,runoob,taobao" delims="," var="name"><c:out value="${name}"/></c:forTokens>

   <c:url>

//<c:url>标签可以正确的对url编码,常配合<c:param>使用
//有value、context、var、scope四个属性
//context用于指定根路径
//路径是/index.jsp
<c:url  value="/index.jsp" context="/"/>
//路径是/store/item.jsp
<c:url  value="/item.jsp" context="/store"/>
//var指定了用于创建和保存URL结果的EL变量,默认作用域是page,通过scope属性更改作用域
<a href="<c:url  value="/index.jsp" var="homeUrl" scope="request"/>">测试url</a>

   <c:redirect>

//<c:redirect>标签通过自动重写URL来将浏览器重定向至一个新的URL,丢失<c:redirect>之后所有内容,可以和<c:param>一起使用
//有url、context两个属性
<c:redirect url="http://www.baidu.com"/>

   <c:import>

//可以获取特定url资源的内容,这些内容可以保存在响应、字符串变量、Reader变量中,可以和<c:param>配合使用,用于指定url中的查询参数
//有url、content、charEncoding、var、scope、varReader六个属性
//url可以使相对路径、绝对路径
//charEncoding,当资源未返回Content-Type时使用
//scope指定var的作用域
//varReader中的Reader变量只能在<c:import>标签内,不可以和var、<c:param>一起使用
//以下代码打印百度源码
<c:import var="data" url="http://www.baidu.com"/>
<c:out value="${data}"/>

  <c:param>

//<c:param>标签用于设定跟url有关参数
//有name、value两个属性
<c:url var="myURL" value="main.jsp">
    <c:param name="name" value="abc"/>
    <c:param name="url" value="www.baidu.com"/>
</c:url>
<a href="/<c:out value="${myURL}"/>">
//输出:/main.jsp?name=abc&url=www.baidu.com

  <c:set>、<c:remove>

//<c:set>标签用于设置变量值和对象属性
//有value、target、property、var、scope五个属性
//修改对象的值
<c:set target="${someObject}" property="someProperty" value="hello"/>
//给某个变量赋值
<c:set var="salary" scope="session" value="${2000*2}"/>

//<c:remove>标签用于移除一个变量,如果未指定作用域,所有名称匹配的都会被移除
//有var、scope两个属性
<c:remove var="salary"/>

格式化标签库(fmt)

  国际化和本地化组件

    国际化和本地化由3个部分组成

    1.文本之间不同语言的转换

    2.日期、时间、货币、百分比的格式化

    3.外国货币和本地货币的转换(通常可忽略)

    在JSTL中提供了i18n标签用于处理文本之间不同语言的转换,提供了格式化标签处理日期、时间、货币、百分比的格式化

//taglib指令
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

  i18n标签

    <fmt:bundle>、<fmt:setBundle>

      i18n标签通过<fmt:setLocale>、<fmt:setBundle>指定资源包

      搜索资源的先后顺序

      1.<fmt:message>中的bundle属性指定的资源包

      2.内嵌在<fmt:bundle>中的资源包

      3.默认的本地化资源包

//定义english资源包
public class Example_En extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = {
  {"count.one", "One"},
  {"count.two", "Two"},
  {"count.three", "Three"},
  };
}
//定义Spanish资源包
public class Example_es_ES extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = {
  {"count.one", "Uno"},
  {"count.two", "Dos"},
  {"count.three", "Tres"},
  };
}
//<fmt:setBundle>用于加载指定的资源包
//属性:basename、var、scope
<fmt:bundle basename="com.test.Example_En">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

//修改地区
<fmt:setLocale value="es_ES"/>
<fmt:bundle basename="com.test.Example_es_ES">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

    <fmt:message>

//<fmt:message>标签用于将bundle里的key替换成对应的value
//属性:key、bundle、var、scope
<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.test.Example" var="lang"/>
<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>  

    <fmt:setLocale>

//<fmt:setLocale>标签用来将给定的区域存储在locale配置变量中,正常情况不需要使用,国际化应用程序会在请求被转发到JSP 中时自动进行区域设置
//属性:value、variant、scope
<fmt:setLocale value="es_ES"/>     

    <fmt:requestEncoding>

    <fmt:requestEncoding>标签用来指定返回给Web应用程序的表单编码类型

    由于Servlet在<fmt:requestEncoding>之前处理请求, Servlet能修改字符编码;现代浏览器会设定字符编码内容类型请求和响应,故现在已经不需要这个标签

  格式化标签

    <fmt:timeZone>、<fmt:setTimeZone>

    格式化标签优先级

    1.格式化标签中的timeZone属性

    2.<fmt:timeZone>标签内

    3.初始化参数中的

    4.JVM的时区

//<fmt:setTimeZone>标签用来复制一个时区对象至指定的作用域
//属性:value、var、scope
<fmt:setTimeZone value="America/Chicago" var="zone"/>
//<fmt:timeZone>标签用来指定时区,供其它标签使用
//属性:value
<fmt:timeZone value="${zone}"></fmt:timeZone>

    <fmt:formatDate>、<fmt:parseDate>

//<fmt:formatDate>标签用于使用不同的方式格式化日期
//属性:value、type、dateStyle、timeStyle、pattern、timeZone、var、scope
//<fmt:formatDate>的value值必须是java.util.Data实例或者EL表达式,不支持java.util.Calendar、及java 8 Date
//type只能是DATE, TIME, 或 BOTH,分别表示只输出日期、只输出时间、输出日期和时间
//dateStyle、timeStyle只能是FULL, LONG, MEDIUM, SHORT, 或 DEFAULT
//pattern用于自定义格式模式,不推荐
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>日期格式化 (1): <fmt:formatDate type="time" 
            value="${now}" /></p>
<p>日期格式化 (2): <fmt:formatDate type="date" 
            value="${now}" /></p>
<p>日期格式化 (3): <fmt:formatDate type="both" 
            value="${now}" /></p>
<p>日期格式化 (4): <fmt:formatDate type="both" 
            dateStyle="short" timeStyle="short" 
            value="${now}" /></p>
<p>日期格式化 (5): <fmt:formatDate type="both" 
            dateStyle="medium" timeStyle="medium" 
            value="${now}" /></p>
<p>日期格式化 (6): <fmt:formatDate type="both" 
            dateStyle="long" timeStyle="long" 
            value="${now}" /></p>
<p>日期格式化 (7): <fmt:formatDate pattern="yyyy-MM-dd" 
            value="${now}" /></p>
/*输出
日期格式化:
日期格式化 (1): 11:19:43
日期格式化 (2): 2018-4-29
日期格式化 (3): 2018-4-29 11:19:43
日期格式化 (4): 18-4-29 上午11:19
日期格式化 (5): 2018-4-29 11:19:43
日期格式化 (6): 2018年4月29日 上午11时19分43秒
日期格式化 (7): 2019-4-29
*/
//<fmt:parseDate>用于解析日期
//属性与<fmt:formatDate>相同
<c:set var="now" value="20-10-2015" />
<fmt:parseDate value="${now}" var="parsedEmpDate" 
                              pattern="dd-MM-yyyy" />
<p>解析后的日期为: <c:out value="${parsedEmpDate}" /></p>

    <fmt:formatNumber>、<fmt:parseNumber>

//<fmt:formatNumber>标签用于格式化数字,百分比,货币
//属性:value、type、pattern、currencyCode、currencySymbol、groupingUsed、maxIntegerDigits、
minIntegerDigits、maxFractionDigits、minFractionDigits、var、scope
//value表示要显示的数字 //type只能是NUMBER,CURRENCY,或 PERCENT类型 //currencyCode、currencySymbol货币码、货币符号 <c:set var="balance" value="120000.2309" /> <p>格式化数字 (1): <fmt:formatNumber value="${balance}" type="currency"/></p> <p>格式化数字 (2): <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (3): <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" /></p> <p>格式化数字 (4): <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" /></p> <p>格式化数字 (5): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (6): <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" /></p> <p>格式化数字 (7): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (8): <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" /></p> /* 数字格式化: 格式化数字 (1): ¥120,000.23 格式化数字 (2): 000.231 格式化数字 (3): 120,000.231 格式化数字 (4): 120000.231 格式化数字 (5): 023% 格式化数字 (6): 12,000,023.0900000000% 格式化数字 (7): 023% 格式化数字 (8): 120E3 */ //<fmt:parseNumber>标签用来解析数字,百分数,货币 <c:set var="balance" value="1250003.350" /> <fmt:parseNumber var="i" type="number" value="${balance}" /> <p>数字解析 (1) : <c:out value="${i}" /></p> <fmt:parseNumber var="i" integerOnly="true" type="number" value="${balance}" /> <p>数字解析 (2) : <c:out value="${i}" /></p>

  函数库(fn)

函数 描述
fn:contains() 测试输入的字符串是否包含指定的子串
fn:containsIgnoreCase() 测试输入的字符串是否包含指定的子串,大小写不敏感
fn:endsWith() 测试输入的字符串是否以指定的后缀结尾
fn:escapeXml() 跳过可以作为XML标记的字符
fn:indexOf() 返回指定字符串在输入字符串中出现的位置
fn:join() 将数组中的元素合成一个字符串然后输出
fn:length() 返回字符串长度
fn:replace() 将输入字符串中指定的位置替换为指定的字符串然后返回
fn:split() 将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回
fn:startsWith() 测试输入字符串是否以指定的前缀开始
fn:substring() 返回字符串的子集
fn:substringAfter() 返回字符串在指定子串之后的子集
fn:substringBefore() 返回字符串在指定子串之前的子集
fn:toLowerCase() 将字符串中的字符转为小写
fn:toUpperCase() 将字符串中的字符转为大写
fn:trim() 移除首位的空白符

猜你喜欢

转载自www.cnblogs.com/vshen999/p/8917115.html
今日推荐