正文

javaWeb笔记(9)2008-04-01 20:05:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ghlat520/33783.html

分享到:

 第九章  Jsp标准标签库

                              什么是Jsp标准标签库
                               1)Jsp标准标签库(javaServer pages standard tag library)简称jstl是jsp已经为我们做好的
                                 可以直接使用的用于编写和开发jsp页面的一组标准标签。
                               2)jstl的最终目的是希望为我们提供一个无脚本环境,在此环境中用户可以使用标签来代替以前的Java程序段。
                                 这样简化的jsp页面的开发,即使一个不懂java程序的用户也可以使用jstl来进行jsp页面的开发
                               3)jstl包含三类标签:
                                   A)核心标签库
                                        11)通用标签
                                        22)条件标签             
                                        33)迭代标签
                                   D)国际化与格式化标签库
                                   E)Sql标签库
                             

                                      核心标签库
                    1)核心标签库由通用标签、条件标签、迭代标签所构成
                    2)通用标签用于操作jsp页面创建的范围变量
                    3)条件标签用于对jsp页面中的代码进行条件判断和处理
                    4)迭代标签用于循环遍历一个对像集合。相当于C#中的for earch
                    5)要使用核心标签库,必须经过两个步骤
                        A)设置工程需要库为JSTL1.1-->选择Project-->ProjectProperty-->RequiredLibrarles-->Add
                        B)必须要导入核心标签库的Url格式如下:                       
                          <%@  taglib  uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>

                                    通用标签
                   1)通用标签用于设置、删除和显示在jsp页面内创建的变量值。
                   2)<c:set var="变量名" value="值" scope="范围"/>用于设置变量值
                      其中的范围是:page,request,session.application,默认为page
                      比如:
                       <c:set var="uid" value="chen" scope="session"/>
                      相当于:<% session.setAttribute("uid","chen"); %>
                      只不过一个是jsp标签,一个是Javascriptlet Java代码段
                   3)<c:out value="${变量名}"/>用于显示变量的值或直接通过EL来进行显示${变量名}
                   4)<c:remove var="变量名" scope="范围"/>
                     
                 条件标签
                   1)条件标签以支持jsp页面中的各种条件,共有两种类型
                      A)<c:if test="条件" >  相当于 if  (条件)
                          内容                       {
                        </c:if>                      }                     
                       举例:
                        1)让用户输入两个数,求出最大值与最小值
                          提示:创建一个页面max.jsp页面,然后提交到dowithmax.jsp页面
                           <c:if test="${param.num1>0+param.num2}">
                               <c:set var="max" value="${param.num1}"/>
                               </c:if>
                           <c:if test="${param.num1<0+param.num2}">
                              <c:set var="max" value="${param.num2}"/>
                           </c:if>
                         最大值是:<c:out value="${max}"/>                     
                        2)让用户输入商品数量、商品单价、选择是否是会员来核算应付款
                          一级会员:98折 二级会员:97折 三级会员:96折
                          提示:创建一个buy.jsp页面,然后创建一个dowithbuy.jsp页面
                           <c:if test="${param.vip==1}">
                              <c:set var="discount" value="0.99"/>
                           </c:if>
                           <c:if test="${param.vip==2}">
                              <c:set var="discount" value="0.98"/>
                           </c:if>
                          <c:if test="${param.vip==3}">
                              <c:set var="discount" value="0.97"/>
                          </c:if>
                         应付款:${param.amount*param.unitprice*discount}
                    B)<c:choose>
                           <c:when test="条件1">
                                内容
                           </c:when>
                           <c:when test="条件2">
                               内容
                           </c:when>                                                      
                                ..............
                           <c:otherwise>
                               内容
                           </otherwise>
                       </c:choose>
                        
         1:修改上页的两例
                       2:让用户输入两个数和一个操作符,求出对应值

                         
                                  迭代标签
                   1)迭代标签相当于C#中的for earch,就是循环。
                   2)迭代标签有两种形式。一种是<forEeach>,另外一种是<forTokens>
                    A):<c:forEeach  var="迭代变量名" items="集合" varStatus="状态变量名">
                        内容
                      </c:forEeach> 
                      说明:
                         var表示变量名相当于foreach(int item in 集合) 中的item
                         items:相当于foreach中的item
                         varStatus:是状态变量,可以省略
                           通常有:
                            ${状态变量名.count}---从1开始的当前迭代项
                            ${状态变量名.index}---从0开始的当前迭代项
                         
                            举例:
                            1)
                             <%
     String[] Car={"彩电","空调","手机","微波炉"};
        %>
                            所有的汔车如下:
                                <c:forEach var="shop" items="<%=Car%>" varStatus="status">
                                    <ul>第${status.count}种商品是${shop}</ul>
                                </c:forEach>
                           2)从数据库中查询出一个ResultSet并存放ArraryList。通过迭代标签进行输出
                             public class shop
                              {
                                   public ArrayList getAllShop()
                                      {        
                                          ArrayList shops = new ArrayList();
                                          ResultSet rs = tagapp.DbManager.RunHasResultSql("select * from shopinfo");
                                          while (rs.next())
                                           {
                                               shop demo = new shop();
                                               demo.setId(rs.getString(1));
                                               demo.setName(rs.getString(2));
                                               demo.setUnitPrice(rs.getInt(3));
                                               shops.add(demo);
                                            }
                                      return shops;
                               }

                                    <jsp:useBean id="demo" scope="session" class="tagapp.shop"/>
                                    <c:forEach var="item" items="<%=demo.getAllShop()%>">
                                    ${item.id} ${item.name} ${item.unitPrice} <br>
                                    </c:forEach>

                           B):<c: forTokens items="以分隔符分开的字符串" delims="分隔符" var="迭代变量名">
                                内容
                              </c:forTokens

                           应用举例:
                            1)
                              <c:set var="shops" value="彩电,空调,热水器"/>
                              <c:forTokens delims="," items="${shops}" var="item">
                               ${item}<br />
                              </c:forTokens>
                           2)从数据库中取出由固定分隔符分开的某个字段的值
                             shop表:
                                   s1-彩电-1200
                                   s2-空调-1300
       s3-微波炉-1500
                          


                                     国际化与格式化标签
                        1)国际化与格式化标签主要是用来实现一个WEB应用程序同时有几种国家的语言
                        2)必须要导入:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt" %>
                        2)原理:
                           A:为每一种语言做一个资源文件。比如:
                               app_en.properties(英文), app_zh.properties(中文)
                               格式形如:键=值
                               app_en.properties文件的内容如下
                                  uid=this my uid
                                  pwd=this my pwd
                                  login=login
                                  reset=reset
                              app_zh.properties文件的内容如下
                                  uid=用户名
                                  pwd=密码
                                  login=登录
                                  reset=重置
                               1:注意资源文件命名的规范:文件名_两个字符标识. properties
                               2:如果是中文为了避免乱码。要通过如下命令进行转换
                                 native2ascii -encoding gb2312 源文件 目的文件
                                  比如:
                                 native2ascii -encoding gb2312 app_zh1.properties(源文件) app_zh1.properties(目的文件)
                               3:资源文件一定要放在WEB-INF\classes下面,classes一定要手动创建******
                             B:通过<fmt:setLocale value="en或zh"/>来决定使用那种语言的资源文件
                             C:通过<fmt:setBundle basename="app"/>来决定要加载那些资源文件
                             D:通过<fmt:message key="pwd" />来取出资源文件里面的值
                             E:注意B,C的顺序不能颠倒
                         举例:
                             1)登录的中文与英文的例子  
                                    <c:if test="${param.language==null || param.language=='en'}">
        <fmt:setLocale value="en"/>
                                    </c:if>
                                    <c:if test="${param.language=='zh'}">
                                    <fmt:setLocale value="zh"/>
                                    </c:if>
                                    <fmt:setBundle basename="app"/>
                                      
                                             Sql标签库
                            1)JSTL的SQL标签用于访问数据库,它提供的各种标签可用于在jsp页面内直接访问
                              数据库,从而减少了JavaScriptlet代码
                            2)要使用sql标签库,必须要导入<%@ taglib uri="http://java.sun.com/jsp/jstl/sql"  prefix="sql" %>
                            3)Sql标签库由以下几种
                               A:设置数据源
                                 格式:<sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:jsp" var="conn"/>
                                 var:给数据源取一个别名,以后其它的sql标签将使用该变量做为数据源进行查询
                                 scope:数据源变量的范围
                               B:对数据库进行更新操作
                                 格式:
                                  <sql:update dataSource="${数据源变量}">
                                   写上更新语句,是不返回结果的(delete,update,insert,create)
                                  </sql:update>
                                 dataSource:指定进行数据更新的连接

                                 比如:                 
                                 <sql:update dataSource="${conn}">
                                  update shopinfo set unitprice=unitprice+100
                                 </sql:update>

                               C:对数据库进行查询
                                 格式:
                                  <sql:query var="shops" dataSource="${conn}"> 
                                     select语句
                                  </sql:query>
                                  var:给查询的结果取一个变量名
                                  dataSource:指定进行数据更新的连接
                                  此标签经常与<forEarch>标签一起使用,来显示查询出的值
                                  在使用<forEarch>时可以使用begin end 来指定需要从那里开始到那里结束****
                               比如:
                                 <sql:query var="shops" dataSource="${conn}">
                                   select * from shopinfo
                                 </sql:query>

                               <c:forEach var="row" items="${shops.rows}">                             
                                     ${row.id}
                                     ${row.name}
                                     ${row.unitprice}
                               </c:forEach>
                               说明:
                                1)<c:forEach var="row" items="${shops.rows}" begin="1"> 从第二行开始,全部取到
                                2)<c:forEach var="row" items="${shops.rows}" begin="1" end="3"> 从第二行到第四行,全部取到

                          D:为sql语句中的参数设置值,它一般是<sql:query>与<sql:update>的子标签
                                格式:<sql:param   value="值"/>
                                比如:
                                 <c:set var="id" value="s1"/>
                                 <sql:query var="shops" dataSource="${conn}">
                                     select * from shopinfo where id=?
                                     <sql:param value="${id}"/>
                                 </sql:query>
                                 
                           
                             举例:
                             1)通过标签从数据库查询出数据,并显示出来
                             2)从表单填写商品信息并添加到数据库中


                             3)让用户从表单中选择部门编号,从另一个页面中显示所有该部门的职工

 

 

                               2)在第一题的基础上进行数据的分页
                                   <c:if test="${conn==null}">
                                   <sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:jsp" var="conn" scope="session"/>
                                   <sql:query var="shops" dataSource="${conn}" scope="session">
                                      elect * from test
                                   </sql:query>
                                    <c:set var="curpage" value="1" scope="session"/> //设置当前页的变量curpage
                                  </c:if>
                                     <c:choose>
                                        c:when test="${shops.rowCount==0}">
                                         没的查询的结果
                                        </c:when>
                                        <c:otherwise> //开始查询
                                            <c:if test="${param.page == 'next'}"> //如果是下一页
                                                <c:if test="${pagesize*curpage<shops.rowCount}"> //如果没有到末尾
                                                   <c:set var="curpage" value="${curpage+1}" scope="session"/>
                                                </c:if>
                                            </c:if>
                                       <c:if test="${param.page=='prio'}"> 如果是上页
                                         <c:if test="${curpage>1}"> //如果没有到首页
                                            <c:set var="curpage" value="${curpage-1}" scope="session"/>
                                         </c:if>
                                        </c:if>
                                       <c:forEach var="row" items="${shops.rows}" begin="${pagesize*(curpage-1)}" end="${pagesize*curpage-1}" >
                                          <tr>
                                              <td><div align="center">${row.id}</div></td>
                                              <td><div align="center">${row.a}</div></td>
                                              <td><div align="center">${row.b}</div></td>
                                           </tr>
                                        </c:forEach>
                                          <a href="jsp1.jsp?page=next">下一页</a> <a href="jsp1.jsp?page=prio">上一页</a>
                                       </c:otherwise>
                                      </c:choose>

阅读(2430) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册