struts1.x-bean标签库实例

 

使用前请在WebRoot下新建一个test.jsp文件,内容随意,例如

hello<br>
<b>test</b>
<%=1%>

在配置文件中新建一个test.hello键值,内容随意,如

test.hello =hello~

配置struts-config.xml,在global-forwards里建立一个

<forward name="go" path="bean.jsp"> </forward>

再在action-mapping里增加一个action:

<action path="/hello" type="com.yourcompany.struts.action.HelloAction" > </action>

建立两个bean:

【User.java】

  1. package com. yourcompany. struts. bean;
  2.  
  3. public class User {
  4.        
  5.         private String name;
  6.        
  7.         private String sex;
  8.        
  9.         private String address;
  10.        
  11.         private Company company;
  12.  
  13.         public String getName ( ) {
  14.                 return name;
  15.         }
  16.  
  17.         public void setName ( String name ) {
  18.                 this. name = name;
  19.         }
  20.  
  21.         public String getSex ( ) {
  22.                 return sex;
  23.         }
  24.  
  25.         public void setSex ( String sex ) {
  26.                 this. sex = sex;
  27.         }
  28.  
  29.         public String getAddress ( ) {
  30.                 return address;
  31.         }
  32.  
  33.         public void setAddress ( String address ) {
  34.                 this. address = address;
  35.         }
  36.  
  37.         public Company getCompany ( ) {
  38.                 return company;
  39.         }
  40.  
  41.         public void setCompany (Company company ) {
  42.                 this. company = company;
  43.         }
  44.  
  45. }

【Company.java】

  1. package com. yourcompany. struts. bean;
  2.  
  3. public class Company {
  4.  
  5.         private String name;
  6.        
  7.         private String address;
  8.  
  9.         public String getName ( ) {
  10.                 return name;
  11.         }
  12.  
  13.         public void setName ( String name ) {
  14.                 this. name = name;
  15.         }
  16.  
  17.         public String getAddress ( ) {
  18.                 return address;
  19.         }
  20.  
  21.         public void setAddress ( String address ) {
  22.                 this. address = address;
  23.         }
  24.        
  25. }

其他不管它,新建bean.jsp,复制以下内容:

  1. <%@ page language= "java" import="java.util.*,com.yourcompany.struts.bean.*" pageEncoding="GBK"%>
  2.  
  3. <%@ taglib uri= "http://struts.apache.org/tags-bean" prefix= "bean" %>
  4. <%@ taglib uri= "http://struts.apache.org/tags-html" prefix= "html" %>
  5. <%@ taglib uri= "http://struts.apache.org/tags-logic" prefix= "logic" %>
  6. <%@ taglib uri= "http://struts.apache.org/tags-tiles" prefix= "tiles" %>
  7.  
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html:html lang= "true">
  11.   <head>
  12.     <html:base />
  13.     <title>taglib: Bean</title>
  14.   </head>
  15.   <body>
  16.     <h2> 1. 用于访问http请求或jsp隐含对象的标签</h2>
  17.    
  18.     <h3>bean:cookie</h3>
  19.     <ul>
  20.         <%Cookie cookie = new Cookie ( "userName", "YOYO" );
  21.             response. addCookie (cookie ); %>
  22.         <bean:cookie id= "myCookie" name= "userName" value= "no" />
  23.         Cookie名稱: $ {myCookie. name }<br>
  24.         Cookie的值: $ {myCookie. value }<br>
  25.     </ul>
  26.    
  27.     <h3>bean:parameter</h3>
  28.     <ul>
  29.         <bean:parameter id= "paramValue" name= "msg" value= "no message" />
  30.         傳遞來的msg值: $ {paramValue }<br>
  31.     </ul>
  32.    
  33.     <h3>bean:header</h3>
  34.     <ul>
  35.         <bean:header id= "userLang" name= "Accept-Language" />
  36.         用戶使用的語言:$ {userLang }<br>
  37.         <bean:header id= "browser" name= "User-Agent" />
  38.         瀏覽器:$ {browser }
  39.     </ul>
  40.    
  41.     <h3>bean:page</h3>
  42.     <ul>
  43.         <bean:page id= "req" property= "request" />
  44.         服務器地址:$ {req. localAddr }<br>
  45.         服務器使用的端口:$ {req. localPort }<br>
  46.         站點路徑:<bean:write name= "req" property= "contextPath" />
  47.     </ul>
  48.    
  49.     <hr>
  50.    
  51.     <h2> 2. 用于定义或输出javabean的bean标签</h2>
  52.     <%
  53.         User user = new User ( );
  54.         user. setName ( "YOYO" );
  55.         user. setAddress ( "cuc.fjnu" );
  56.         user. setSex ( "female" );
  57.         Company company = new Company ( );
  58.         company. setName ( "SPOTO" );
  59.         company. setAddress ( "Fuzhou" );
  60.         user. setCompany (company );
  61.        
  62.         pageContext. setAttribute ( "userInfo",user );
  63.      %>
  64.    
  65.     <h3>bean:define</h3>
  66.     <ul>
  67.         <li>定義一個新的bean</li><br>
  68.         <bean:define id= "newstr" value= "hello,world!"></bean:define>
  69.                 $ {newstr }<br>
  70.         <br>
  71.        
  72.         <li>根據已有的bean,創建新的bean</li><br>
  73.         <bean:define id= "newUserInfo" name= "userInfo" scope= "page" type= "com.yourcompany.struts.bean.User"></bean:define>
  74.         姓名:$ {newUserInfo. name }<br>
  75.         性別:$ {newUserInfo. sex }<br>
  76.         <br>
  77.        
  78.         <li>根據已有的bean的屬性創建新的bean</li><br>
  79.         <bean:define id= "userAdd" name= "userInfo" property= "address"></bean:define>
  80.         地址:$ {userAdd }<br>
  81.         <br>
  82.        
  83.     </ul>
  84.    
  85.     <h3>bean:write</h3>
  86.     <ul>
  87.         <li>輸出數字</li><br>
  88.         <% request. setAttribute ( "number", new Double ( 1234567.89 ) ); %>
  89.         米有格式:<bean:write name= "number" scope= "request"/><br>
  90.         使用 "###,###.####"格式輸出:<bean:write name= "number" scope= "request" format= "###,###.####" /><br>
  91.         使用 "000,000.0000"格式輸出:<bean:write name= "number" scope= "request" format= "000,000.0000" /><br>
  92.         <br>
  93.        
  94.         <li>輸出日期</li><br>
  95.         <% pageContext. setAttribute ( "today", new Date ( ) ); %>
  96.         默認輸出:<bean:write name= "today" /><br>
  97.         用 "yyyy-MM-dd"格式輸出:<bean:write name= "today" format= "yyyy-MM-dd" /><br>
  98.         <br>
  99.        
  100.         <li>輸出 HTML字符串</li><br>
  101.         <% pageContext. setAttribute ( "htmlstr", "<font color=red>redfont</font>" ); %>
  102.         默認輸出:<bean:write name= "htmlstr" /><br>
  103.         filter為 true時的輸出:<bean:write name= "htmlstr" filter= "true" /><br>
  104.         filter為 false時的輸出:<bean:write name= "htmlstr" filter= "false" /><br>
  105.         <br>
  106.        
  107.         <li>輸出bean</li><br>
  108.         姓名:<bean:write name= "userInfo" property= "name" /><br>
  109.         地址:<bean:write name= "userInfo" property= "address" /><br>
  110.         性別:<bean:write name= "userInfo" property= "sex" /><br>
  111.         公司:<bean:write name= "userInfo" property= "company.name" /><br>
  112.         公司地址:<bean:write name= "userInfo" property= "company.address" /><br>
  113.         <br>
  114.        
  115.         <li>使用ignore屬性:當對象為 null時不會報錯</li><br>
  116.         <bean:write name= "novalue" ignore= "true" />
  117.        
  118.     </ul>
  119.    
  120.     <h3>bean:size</h3>
  121.     <%
  122.         List list = new ArrayList ( );
  123.         list. add ( "hello" );
  124.         list. add ( "test" );
  125.         pageContext. setAttribute ( "strlist",list );
  126.      %>
  127.     <bean:size id= "length" name= "strlist" scope= "page" />
  128.     <ul>獲得list的長度為$ {length }</ul>
  129.    
  130.     <hr>
  131.    
  132.     <h2> 3. 用于访问web应用资源的bean标签</h2>
  133.    
  134.     <h3>bean:resource</h3>
  135.     <ul>
  136.         <!-- 自己寫一個test. jsp -->
  137.         <li>默認將它讀取為字符串</li><br>
  138.         <bean:resource id= "inputResource" name= "test.jsp" />
  139.         <bean:write name= "inputResource" /><br>
  140.         <br>
  141.        
  142.         <li>作為 InputStream讀</li><br>
  143.         <bean:resource id= "inputstream" name= "test.jsp" input= "yes" />
  144.         <bean:write name= "inputstream" /><br>
  145.         <br>
  146.        
  147.     </ul>
  148.    
  149.     <h3>bean:struts</h3>
  150.     <ul>
  151.         <!-- 在struts-config. xml里配一個global-forward,name為go -->
  152.         <li>forward</li><br>
  153.         <bean:struts forward= "go" id= "fwd_go"/>
  154.         forward=go時的path:<bean:write name= "fwd_go"  property= "path"/><br>
  155.         <br>
  156.        
  157.         <!-- 在struts-config. xml里配一個action-mapping,path為/hello -->
  158.         <li>mapping</li><br>
  159.         <bean:struts mapping= "/hello" id= "map" />
  160.         /hello. do時的action:<bean:write name= "map" property= "type" /><br>
  161.         <br>
  162.        
  163.     </ul>
  164.    
  165.     <h3>bean:include</h3>
  166.     <ul>
  167.         <!-- 自己寫一個test. jsp -->
  168.         <bean:include id= "includefile" page= "/test.jsp" />
  169.         <bean:write name= "includefile" />
  170.     </ul>
  171.    
  172.     <h3>bean:message</h3>
  173.     <ul>
  174.         <!-- 要用時請先在配置文件里加上test. hello鍵值 -->
  175.         <bean:message key= "test.hello" />
  176.     </ul>
  177.    
  178.   </body>
  179. </html:html>

详细可见内容注释。部署好后即可运行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值