JSTL使用总结(2) fmt标签库和fn标签库

来源:互联网 发布:域名劫持软件 编辑:程序博客网 时间:2024/06/10 10:37

五、fmt标签库


此标签库的作用是进行国际化操作,比如时间格式、数字格式的转换、地区的转换等;


1.国际化知识补充


zh_CN   中国

en_US 美国

fr_FR 法国


2.<fmt:setLocale>


用于确定区域;一般结构如下:

<fmt:setLocale value="    " />

value表示指定区域,比如zh_CN;


3.<fmt:requestEncoding>


用于设定编码;一般结构如下:

<fmt:requestEncoding value="   "/>

value用于指定编码,比如GBK;


4.<fmt:setBundle>

<fmt:setBundle>、<fmt:message>都是用来操作资源文件的。
 
注意: 资源文件放在WEB-INF/classes下!
 

设定资源文件;一般结构如下:

<fmt:setBundle basename="   "    var="    "/>

basename表示资源文件名称,不需要properties后缀,var表示设定资源文件的属性名称;

var在<fmt:message bundle="">属性中使用。

比如:

<fmt:setBundle basename="1" var="pm"/>

<fmt:message bundle="${pm}"/>


5.<fmt:message>


获得资源文件某个特定键对应的值;一般结构如下:

<fmt:message key="    "    var="     "   bundle="     " />

key用于指定键;

var保存value的值;

bundle表示资源文件;


6.<fmt:formatNumber>


用于格式化数字;一般结构如下:

<fmt:formarNumber value="     "    var="     "     maxIntegerDigits="    "     maxFractionDigits="     "     groupingUsed="     "    />

value表示需要被格式化的值;

var保存格式化的值;

maxIntegerDigits用于指定整数部分的位数;

maxFractionDigits用于指定小数部分的位数;

groupingUsed用于指定是否每3位一个","分割;true 或false


7.<fmt:parseNumber>


解析数字;一般结构如下:

<fmt:parseNumber value="    "    var="     "  />


8.<fmt:formatDate>


格式化日期;一般结构如下:

<fmt:formatDate value = "     "     type="    "   pattern="     "   var= "     " />

type表示格式化日期或时间或全部格式化,date、both、time;

pattern表示格式化的格式;

var保存结果;


9.<fmt:parseDate>


解析日期;一般结构如下:

<fmt:parseDate value="    "pattern="      "    var="      "/>


    <%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%>      <%@ taglib prefix="fmt" uri="jstl/fmt"%>      <jsp:useBean id="per" class="org.person.Person" scope="page"/>      <html>          <head>              <title></title>          </head>          <body>              <%                  pageContext.setAttribute("date" ,new java.util.Date());              %>              <fmt:setLocale value="zh_CN"/>              <fmt:requestEncoding value="GBK"/>              <fmt:formatDate value="${date}" type="both" pattern="yyyy-MM-dd hh:mm:ss,SSS" var="resultdate"/>              <h3>${resultdate}</h3>              <fmt:setBundle basename="xiazdong" var="msg"/>              <fmt:message key="name" var="nameref" bundle="${msg}"/>              <h3>${nameref}</h3>              <fmt:formatNumber value="1234567.1234567" maxIntegerDigits="5" maxFractionDigits="5" groupingUsed="true" var="num1"/>              <h3>${num1}</h3>              <fmt:parseNumber value="${num1}" var="num2"/>              <h3>${num2}</h3>              <fmt:parseDate value="2011-10-10" pattern="yyyy-MM-dd" var="date2"/>              <h3>${date2}</h3>          </body>      </html>  

六、fn标签库


此标签库的标签类似于String提供的方法;


1.${fn:contains(str, " var   ")}        字符串是否存在var

2.${fn:containsIgnoreCase(str,"  var ")}  字符串是否存在var(忽略大小写)

3.${fn:startsWith(str," var   ")}字符串是否以var开头

4.${fn:endsWith(str,"  var  ")}字符串是否以var结尾

5.${fn:substring(str,int begin,int end)};截取字符串从begin-end-1

6.${fn:trim(str)} 去掉左右空格

7.${fn:toUpperCase(str)}全变成大写

8.${fn:replace(str," var1  " ," var2 ")};把var1代替成var2

9.${fn:split(str,"  , ")[i]}以“,”划分,并取出第2个

10.${not empty date && fn:split(queryDate,':')[0] eq date }非空且第1个元素等于date

11、列表list_huiYiHuanDeng第1个元素(下标为0)的值

${list_huiYiHuanDeng[0].huiyi_typeName }


七、forEach标签奇偶性判断

<c:forEach var="var" items="list" varStatus="status" begin="1" step="1">
  <li <c:if test="${status.count%2==1 }"> class="yourcss" </c:if>>
</c:forEach>
 

八、forEach标签奇偶性判断

  1. <c:set var="username_sp" value="<s:property value=\"user.userName.equals('admin')\"/>/>  
  2.   
  3. <c:if test="${username_sp== true }"> 


0 0
原创粉丝点击