structs中的四个标签使用

struts bean标签的一些简单使用
<jsp:useBean id="对象名称" scope="保存范围(四种属性范围)" class="包.类"/>
<jsp:setProperty name="id(jsp:useBean中使用)" property="属性"/>
<jsp:getProperty name="id" property="属性"/>


Bean标签:
<bean:define>标签
  定义或复制一个对象的
  定义对象一般为String类型
  复制对象——>迭代标签
  <bean:define id="str" value="hell wp ,,,,"/>
    <h1>${str}</h1>
      如果可以通过EL进行访问,则表示“str”保存在了四种属性范围之中,等同与下面的语句
      String str="hell wp.....";
      pageContext.setAttribute("str",str);
      如果不使用EL该如何输出呢?通过Struts中的<bean:write>进行打印
      <bean:write name="str"/>
      ★:id : 就表示一个存放四种属性范围之中的名称
      ★:name:表示使用一个存放四种属性范围中的对象
    
 
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
      <title>beanDemo01.jsp</title>
  </head>
 
  <body>
   <bean:define id="str" value="hell wp ,,,,"/>
    <h1>${str}</h1>
    <h1><bean:write name="str"/></h1>
  </body>
</html:html>

 
<bean:size>标签
   I.求出长度:数组、Collection、Map
   标签肯定数据存放在四种属性范围之中
   求长度的时候,既可以是Map也可以是Collection,但是对于JSP调用的代码是不变的 ;
 

<%@ page language="java" pageEncoding="gb2312"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
      <title>beanDemo02.jsp</title>
  </head>
 
  <body>
  <%
  Map m=new HashMap();
  m.put("a","one");
  m.put("b","two");
  m.put("c","three");
  Collection col=new ArrayList();
  col.add("one");
  col.add("two");
 col.add("three");
 //将Map对象保存在四种属性范围之中
 request.setAttribute("mapa",m);
 request.setAttribute("cola",col);
 
  %>
  <bean:size id="len" name="mapa" scope="request"/>
    <bean:size id="lencol" name="cola" scope="request"/>
  <h1>长度:${len}</h1>
   <h1>长度:${lencol}</h1>
  </body>
</html:html>

<bean:write>标签
   可以打印对象,也可以打印对象中的属性
   在JSP2.0中可以使用EL代替Struts中的<bean:write>标签
 

<%@ page language="java" pageEncoding="gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="cn.java.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
      <title>beanDemo02.jsp</title>
  </head>
 
  <body>
 <jsp:useBean id="sim" class="cn.java.SampleBean" scope="request"/>
 <jsp:setProperty name="sim" property="name" value="上课"/>
  <jsp:setProperty name="sim" property="password" value="123"/>
 <h1>使用EL:</h1>
 <h2>姓名:${sim.name}</h2>
  <h2>密码:${sim.password}</h2>
  <hr>
<h1>使用bean标签</h1>
 <h2>姓名:<bean:write name="sim" property="name" scope="request"/></h2>
   <h2>密码:<bean:write name="sim" property="password" scope="request"/></h2>
  </body>
</html:html>

<bean:message>标签
   在<bean:message>标签中提供了一个占位功能,在输出的文件中占着一位,这一位的数据等待
   <h1>\u6b22\u8fce {0}\u5149\u4e34</h1>
 

<%@ page language="java" pageEncoding="gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="cn.java.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
      <title>beanDemo02.jsp</title>
  </head>
 
  <body>
<bean:message key="welcome" arg0="wpskl"/>
  </body>
</html:html>
logic篇
1.  logic:empty
          该标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:

         1)当Java对象为null时;

         2)当String对象为""时;

         3)当java.util.Collection对象中的isEmpty()返回true时;

         4)当java.util.Map对象中的isEmpty()返回true时。
          eg.
            <logic:empty   name="userList">  
              ...  
           </logic:empty>
           该句等同于:
           if   (userList.isEmpty())   {  
                 ...  
           }  
   2.  logic:notEmpty
          该标签的应用正好和logic:empty标签相反,略。
   3. logic:equal
          该标签为等于比较符。
          eg1. 比较用户的状态属性是否1,若为1,输出"启用";
                 <logic:equal   name="user"   property="state"   value="1">
                     启用
                 </logic:equal>
         eg2. 如果上例中的value值是动态获得的,例如需要通过bean:write输出,因struts不支持标签嵌套,可采用EL来解决该问题。
                <logic:equal   name="charge"   property="num"   value="${business.num}">  
                    ......
                </logic:equal>
    4. logic:notEqual
          该标签意义与logic:equal相反,使用方法类似,略。
    5. logic:forward
          该标签用于实现页面导向,查找配置文件的全局forward。
          eg. <logic:forward name="index"/>
    6. logic:greaterEqual
          为大于等于比较符。
          eg. 当某学生的成绩大于等于90时,输出“优秀”:
               <logic:greaterEqual name="student" property="score" value="90">
                  优秀
            </logic:greaterEqual>
    7. logic:greaterThan
          此为大于比较符,使用方法同logic:greaterEqual,略;
    8. logic:lessEqual
          此为小于等于比较符,使用方法同logic:greaterEqual,略;
    9. logic:lessThan
          此为小于比较符,使用方法同logic:greaterEqual,略;
    10. logic:match
          此标签比较对象是否相等;
          eg1. 检查在request范围内的name属性是否包含"amigo"串:
            <logic:match name="name" scope="request" value="amigo">
                  <bean:write name="name"/>中有一个“amigo”串。
            </logic:match>
         eg2. 检查在request范围内的name属性是否已“amigo”作为起始字符串:
           <logic:match name="name" scope="request" value="amigo" location="start">
               <bean:write name="name"/>以“amigo”作为起始字符串。
            </logic:match>
         eg3.
            <logic:match header="user-agent" value="Windows">
               你运行的是Windows系统
            </logic:match>
    11.  logic:notMatch

 

          此标签用于比较对象是否不相同,与logic:match意义相反,使用方法类似,略。
     12. logic:messagePresent
          该标签用于判断ActionMessages/ActionErrors对象是否存在;
          eg. 如果存在error信息,将其全部输出:
               <logic:messagePresent property="error">
                  <html:messages property="error" id="errMsg" >
                        <bean:write name="errMsg"/>
                  </html:messages>  
               </logic:messagePresent >
     13. logic:messagesNotPresent
          该标签用于判断ActionMessages/ActionErrors对象是否不存在,使用方法与logic:messagePresent类似,略
      14. logic:present
           此标签用于判断request对象传递参数是否存在。
           eg1. user对象和它的name属性在request中都存在时,输出相应字符串:
              <logic:present name="user" property="name">
                  user对象和该对象的name属性都存在
            </logic:present>
          eg2. 若有一个名字为“user”的JavaBean,输出对应字符串:
             <logic:present name="user" >
                  有一个名字为“user”的JavaBean。
            </logic:present>
          eg3.
            <logic:present header="user-agent">
                  we got a user-agent header.
            </logic:present>
      15. logic:notPresent
           此标签用于判断request对象传递参数是否不存在,意义与了logic:present相反,使用方法类似,略。
      16. logic:redirect
           该标签用于实现页面转向,可传递参数。
           eg1. <logic:redirect href="http://www.chinaitlab.com"/>
      
       17. logic:iterator
            用于显示列表为collection的值(List ,ArrayList,HashMap等)。
            eg1. 逐一输出用户列表(userlList)中用户的姓名:
               <logic:iterate  id="user" name="userList">
                  <bean:write name="user" property="name"/><br>
               </logic:iterate>
            eg2. 从用户列表中输出从1开始的两个用户的姓名
               <logic:iterate  id="user" name="userList" indexId="index"  offset="1" length="2">
                  <bean:write name="index"/>.<bean:write name="user" property="name"/><br>
               </logic:iterate>
            eg3. logic:iterator标签的嵌套举例
                <logic:iterate id="user" indexId="index" name="userList">
                       <bean:write name="index"/>. <bean:write name="user" property="name"/><br>
                       <logic:iterate id="address" name="user" property="addressList" length="3" offset="1">
                           <bean:write name="address"/><br>
                       </logic:iterate>
               </logic:iterate>
tiles,俗称"小部件"。由于当今界面设计的可用性要求界面必须保持一致。早期开发使用文件包含的方式来使页面模块化,类型<%@include file=""%> <jsp:include page="">之类的包含指令或jsp标签。这样导致所有包含这些的页面都必须添加类似代码。(tiles类型asp.net2.0的masterpage)
   Q:tiles比包含方式有哪些优先点?
   A:tiles可替换,扩展,继承,代码量小,是否还有别的区别必须深入研究。
   配置tiles
   一、先配置web.xml
在myeclipse上加入struts支持,在WEB-INF下会出现一系列的tld,例struts-bean.tld.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<jsp-config>
<taglib>
<taglib-uri>/tags/tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld </taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld </taglib-location>
 </taglib> <taglib>
<taglib-uri>/tags/html.tld</taglib-uri>
 <taglib-location>/WEB-INF/html.tld </taglib-location> </taglib>
</jsp-config>
</web-app>
这些配置仅仅是为了方便使用。
   webroot下增加如下结构的文件夹
   webroot
      --tiles
         --layout
      --css
      --page
   并分别放header.html,body.html,footer.html放在webroot-tiles文件夹下,都只包括片段代码类似<h1>this is header html!</h1>
二、第一种tiles
   1)定义一个模板页simpletemplate.jsp放在webroot-tiles-layout文件夹下
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="/tags/tiles.tld" prefix="tiles" %>
<%@ taglib uri="/tags/html.tld" prefix="html" %>
<%@ taglib uri="/tags/bean.tld" prefix="bean" %>//web.xml中定义好的taglib-uri只是为了这里方便使用,如果这里直接用/WEB-INF/struts-bean.tld也可以了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head></head><body><div><tiles:insert attribute="header"/></div>//页头<div><tiles:insert attribute="body"/></div>页内容<div><tiles:insert attribute="footer"/></div>页脚</body></html>
    2)定义一个使用该模板的页面simple.jsp放在webroot-page文件夹下
<%@ page  pageEncoding="GBK"%>
<%@ taglib uri="/tags/tiles.tld" prefix="tiles" %>
<tiles:insert   page="/tiles/layout/template.jsp"   flush="true">
<tiles:put name="header" type="page"  value="/tiles/header.html" /> //如果在模板内有的属性在此处没有定义,则出错(如要忽略该错误,必须在simpletemplate.jsp中的<tiles:insert attribute="header" ignore="ture"/>加上ignore="true")
<tiles:put name="body" type="page"  value="/tiles/body.html" /> //或者可以使用<tiles:put name="body" type="string" value="this is a body">;value=(这里可以用expressionlanguage或者html代码)
<tiles:put name="footer" type="page"   value="/tiles/footer.html" />
</tiles:insert>

这样就定义完成一个简单的tiles应用

三、按照第二中的tiles,如果要添加一个页面 都必须要添加一个内容页面,类似body.html,为了避免页面越来越多,可以使用body-wrap技术。
<tiles:put name="body"><input type="button" value="test" tile="123"/></tiles:put>//这样就少建立一个body文件,body内容直接包含在<tiles:input></tiles:input>中了
四、定义,扩展
<%@ page contentType="text/html; charset=GBK" language="java"
    errorPage=""%>
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
    <html:base />

    <title>bookList.jsp</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>
    <p align="center">
        书籍列表
    </p>
    <table border="2" align="center" cellpadding="1" cellspacing="1"
        bordercolor="#CCCCCC">
        <tr>
            <th>
                书名
            </th>
            <th>
                修改
            </th>
            <th>
                删除
            </th>
        </tr>
        <logic:iterate id="book" name="bookList" type="com.ascent.bean.Book">
            <tr>
                <td>
                    <a
                        href="<%=request.getContextPath()%>/bookQuery.do?act=getBook&bookId=<%=book.getBookId() %>"><bean:write
                            name="book" property="bookName" />
                    </a>
                </td>
                <td>
                    <a
                        href="<%=request.getContextPath()%>/bookQuery.do?act=updateBookForm&bookId=<%=book.getBookId() %>">修改</a>
                </td>
                <td>
                    <a
                        href="<%=request.getContextPath()%>/bookQuery.do?act=removeBook&bookId=<%=book.getBookId() %>">删除</a>
                </td>
            </tr>
        </logic:iterate>
    </table>
    <p align="center">
        <a href="javascript:history.back(-1)">返回上一页</a>
    </p>
</body>
</html:html>


 

posted on 2008-05-05 22:54  小顾问  阅读(1657)  评论(0)    收藏  举报