• 小峰servlet/jsp(7)jstl国际化标签库、sql标签库等


    一、jstl国际化标签库:

    fmt:setLocale          设定用户所在的区域;

    fmt:formatDate           对日期进行格式化

    fmt:requestEncoding      设置所有的请求编码;

    fmt:bundle fmt:message    读取国际化资源;

    fmt:formatNumber        格式化数字;

    fmt:timeZone          设置临时时区

    fmt:setLocale:

     1 <body>
     2 <%
     3     pageContext.setAttribute("date",new Date());
     4 %>
     5 中文日期:
     6 <fmt:setLocale value="zh_CN"/>
     7 <fmt:formatDate value="${date }"/>
     8 <hr/>
     9 英文日期:
    10 <fmt:setLocale value="en_US"/>
    11 <fmt:formatDate value="${date }"/>
    12 </body>
    View Code

    fmt:requestEncoding:

    1 <body>
    2 <fmt:requestEncoding value="UTF-8"/>
    3 </body>
    View Code

    fmt:bundle; fmt:message:

    资源文件:

    info_en_US.properties:

    1 name=xiaofeng
    2 info=Current user{0}:Welcome to use our system
    View Code

    info_zh_CN.properties:

    1 name=u5c0fu950b
    2 info=u5f53u524du7528u6237{0}:u6b22u8fceu4f7fu7528u672cu7cfbu7edf
    View Code
     1 <body>
     2 <fmt:setLocale value="zh_CN"/>
     3 <fmt:bundle basename="info">
     4     <fmt:message key="name" var="userName"/>
     5 </fmt:bundle>
     6 <h2>姓名:${userName }</h2>
     7 <fmt:bundle basename="info">
     8     <fmt:message key="info" var="infomation">
     9         <fmt:param value="<font color='red'>小锋</font>"/>
    10     </fmt:message>
    11 </fmt:bundle>
    12 <h2>信息:${infomation }</h2>
    13 <hr/>
    14 <fmt:setLocale value="en_US"/>
    15 <fmt:bundle basename="info">
    16     <fmt:message key="name" var="userName"/>
    17 </fmt:bundle>
    18 <h2>姓名:${userName }</h2>
    19 <fmt:bundle basename="info">
    20     <fmt:message key="info" var="infomation">
    21         <fmt:param value="<font color='red'>小锋</font>"/>
    22     </fmt:message>
    23 </fmt:bundle>
    24 <h2>信息:${infomation }</h2>
    25 </body>
    View Code

    //<fmt:param value="<font color='red'>小锋</font>"/>是为properties中的占位符动态塞数据;

    fmt:formatNumber:

    1 <body>
    2 <!-- value:数值 ;  type:数值类型;  pattern:格式 -->
    3 <fmt:formatNumber value="12" type="currency" pattern="$.00"/> 
    4 <fmt:formatNumber value="12" type="currency" pattern="$.0#"/>
    5 <fmt:formatNumber value="1234567890" type="currency"/> 
    6 <fmt:formatNumber value="123456.7891" pattern="#,#00.0#"/>
    7 </body>
    View Code

    fmt:formatDate:

     1 <body>
     2 <!-- value:数值 ;  type:数值类型;  pattern:格式 -->
     3 <%
     4     Date date=new Date();
     5     pageContext.setAttribute("date",date);
     6 %>
     7 <fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
     8 <hr/>
     9 <fmt:formatDate value="${date }" pattern="yyyy-MM-dd"/>
    10 </body>
    View Code

    fmt:timeZone:

     1 <body>
     2 <!-- value:数值 ;  type:数值类型;  pattern:格式 -->
     3 <%
     4     Date date=new Date();
     5     pageContext.setAttribute("date",date);
     6 %>
     7 当前时间:<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
     8 <hr/>
     9 格林尼治时间:
    10 <fmt:timeZone value="GMT">
    11    <fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
    12 </fmt:timeZone>
    13 </body>
    View Code

    二、jstl SQL标签库:

    sql:setDataSource            设置JDBC连接;

    sql:query                 数据库查询操作

    sql:update                 数据库添加、修改、删除操作;

    sql:transaction               数据库事务

    sql:setDataSource/sql:query:

    数据库中存在这样的表:t_student:

    jsp页面要引入:<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 <h1>设置JDBC连接</h1>
    12 <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456" />
    13 <sql:query var="result">
    14     select * from t_student;
    15 </sql:query>
    16 <h2>总记录数:${result.rowCount }</h2>
    17 <table>
    18     <tr>
    19         <th>编号</th>
    20         <th>学号</th>
    21         <th>姓名</th>
    22         <th>出生日期</th>
    23         <th>性别</th>
    24     </tr>
    25     <c:forEach var="student"  items="${result.rows }">
    26     <tr>
    27         <td>${student.id }</td>
    28         <td>${student.stuNo }</td>
    29         <td>${student.stuName }</td>
    30         <td>${student.birthday }</td>
    31         <td>${student.sex }</td>
    32     </tr>
    33     </c:forEach>
    34 </table>
    35 </body>
    36 </html>
    View Code

    访问展示:

          

    sql:update:

    增:

    1 <body>
    2 <h1>设置JDBC连接</h1>
    3 <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
    4 <h1>添加数据</h1>
    5 <sql:update var="result" >
    6     insert into t_student values(null,"008","草泥马","1991-1-1","男");
    7 </sql:update>
    8 </body>
    View Code

    改:

    1 <body>
    2 <h1>设置JDBC连接</h1>
    3 <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
    4 <h1>修改数据</h1>
    5 <sql:update var="result" >
    6     update t_student set stuNo="010",sex="未知" where id=6
    7 </sql:update>
    8 </body>
    View Code

    删:

    1 <body>
    2 <h1>设置JDBC连接</h1>
    3 <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
    4 <h1>删除数据</h1>
    5 <sql:update var="result" >
    6     delete from t_student where id=6
    7 </sql:update>
    8 </body>
    View Code

    sql:transcation:

     1 <body>
     2 <h1>设置JDBC连接</h1>
     3 <sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
     4 <h1>事务</h1>
     5 <sql:transaction>
     6     <sql:update var="result" >
     7         insert into t_student values(null,"008","草泥马","1991-1-1","男");
     8     </sql:update>
     9 </sql:transaction>
    10 </body>
    View Code

    三、jstl xml标签库:

    x:parse                 解析xml;

    x:out                  输出xml文件的内容

    x:set                  把xml读取的内容保存到指定的属性范围

    x:if                   判断指定路径的内容是否符合判断的条件;

    x:choose/x:when/x:otherwise        多条件判断

    x:forEach                遍历

    需要引入xalan.jar;

    jsp文件需要引入:<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

    有这样两个xml文件,usersInfo.xml; usersInfo2.xml:

    usersInfo.xml:

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <users>
    3     <user>
    4         <name id="n1">张三</name>
    5         <birthday>2011-1-1</birthday>
    6     </user>
    7 </users>
    View Code

    usersInfo2.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <users>
     3     <user>
     4         <name id="n1">张三</name>
     5         <birthday>2011-1-1</birthday>
     6     </user>
     7     <user>
     8         <name id="n2">王五</name>
     9         <birthday>2011-1-2</birthday>
    10     </user>
    11     <user>
    12         <name id="n3">赵六</name>
    13         <birthday>2011-1-3</birthday>
    14     </user>
    15 </users>
    View Code

    文件存放路径:

    1)x:parse   x:out

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 <c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
    12 <x:parse var="usersInfoXml" doc="${usersInfo }"/>
    13 
    14 <h2>姓名:<x:out select="$usersInfoXml/users/user/name"/>
    15 (ID:<x:out select="$usersInfoXml/users/user/name/@id"/>)</h2>
    16 <h2>出生日期:<x:out select="$usersInfoXml/users/user/birthday"/></h2>
    17 </body>
    18 </html>
    View Code

    2) x:set

    1 <body>
    2 <c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
    3 <x:parse var="usersInfoXml" doc="${usersInfo }"/>
    4 
    5 <x:set var="userInfoXml" select="$usersInfoXml/users/user"/>
    6 <h2>姓名:<x:out select="$userInfoXml/name"/></h2>
    7 </body>
    View Code

    3) x:if

    1 <body>
    2 <c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
    3 <x:parse var="usersInfoXml" doc="${usersInfo }"/>
    4 <x:if select="$usersInfoXml/users/user/name/@id='n1'">
    5     <h2>有编号是n1的user信息</h2>
    6 </x:if>
    7 </body>
    View Code

    4) x:choose/x:when/x:otherwise

     1 <body>
     2 <c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
     3 <x:parse var="usersInfoXml" doc="${usersInfo }"/>
     4 <x:choose>
     5     <x:when select="$usersInfoXml/users/user/name/@id='n2'">
     6         <h2>有编号是n2的user信息</h2>
     7     </x:when>
     8     <x:otherwise>
     9         <h2>没有编号是n2的user信息</h2>
    10     </x:otherwise>
    11 </x:choose>
    12 </body>
    View Code

    5) x:forEach:

    1 <body>
    2 <c:import var="usersInfo" url="usersInfo2.xml" charEncoding="UTF-8"/>
    3 <x:parse var="usersInfoXml" doc="${usersInfo }"/>
    4 <x:forEach select="$usersInfoXml/users/user" var="userInfoXml">
    5     <h2>姓名:<x:out select="$userInfoXml/name"/>&nbsp;出生日期:<x:out select="$userInfoXml/birthday"/></h2>
    6     <hr/>
    7 </x:forEach>
    8 </body>
    View Code

    四、jstl函数标签库:

    jsp页面需要引入:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11     pageContext.setAttribute("info","www.java1234.com");
    12 %>
    13 <h2>查找java1234位置:${fn:indexOf(info,"java1234")}</h2>
    14 <h2>判断java1234是否存在:${fn:contains(info,"java1234")}</h2>
    15 <h2>截取:${fn:substring(info,0,5)}</h2>
    16 <h2>拆分:${fn:split(info,".")[1]}</h2>
    17 </body>
    18 </html>
    View Code

    页面展示:

    函数标签一些:

    <c:if test="${fun:length(operation.rangeList) > 0}">

  • 相关阅读:
    联赛前第五阶段总结
    陶陶摘苹果 —— 线段树维护单调栈
    联赛前第三阶段总结
    联赛前第四阶段总结
    [NOIP
    超级跳马 —— 矩阵快速幂优化DP
    我的博客园美化
    Wedding —— 2-SAT
    C++运算符优先级
    water——小根堆+BFS
  • 原文地址:https://www.cnblogs.com/tenWood/p/6506650.html
Copyright © 2020-2023  润新知