文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Using JavaBeans Components in JSP

Using JavaBeans Components in JSP

时间:2008-08-05  来源:zyme007

一、What is a Bean?
    A bean is simply a java class desinged according to the set of guidelines defined by the JavaBean specification.
    1、some notice about bean:
       a)、A bean class must have a no-argument constructor.This allow a tool to create any bean in a generic fashion knowing just the class name.
       b)、The bean properties are accessed through getter and setter methods.
       c)、the bean class should implement the java.io.Serializable or the java.io.Externalizable interface to allow tool to save and restore the bean's state.
    2、the benifit about using a bean
       the nice thing about using a bean is that it can encapsulate all information about the item it represents in one simple package.Another benifit of using a bean is that the bean can encapsulate all the rules about its properties.

二、declaring a Bean in a JSP page
  <html>
  <head>
    <title>A dose of Dilbert</title>
  </head>
  <body bgcolor="white">
    <h1>A dose of Dilbert</h1>
 
    <jsp:useBean id="cartoon"
      class="com.ora.jsp.beans.motd.CartoonBean" />
    <img src="images/<jsp:getProperty name="cartoon"
      property="fileName" />">  
  </body>
  </html>

  <jsp:useBean>is to declare the bean.The action creates an instance of the bean class specified by the class attribute and associated it with the name specified by the 'id' attribute.

三、Reading Bean properties
    A bean's data is represented by it's property.There are two ways to insert a bean property value in a JSP page.
    1、using the <jsp:getProperty> Action
       Once you have created a bean and given it a name using <jsp:useBean> action,you can get the bean's property values with another JSP standard action,named <jsp:getProperty>.This action obtains the current value of a bean property and insert it directly into the response body.
       <jsp:getProperty name="cartoon" property="fileName" />

      the code:<img src="images/<jsp:getProperty name="cartoon"
      property="fileName" />">  looks a bit strange: an element(<jsp:getProperty>) is used as the value of another element's attribute(the <img> tag's src attribute).
      NOTE:this doesn't mean you can use an action element as the value of another JSP action element attribute.Using it to set an HTML element attribute works only because the HTML element isn't recognized as an element by the container.To set a JSP action attribute to the value produced by another action,you must use the <jsp:attribute>standard action instead:
      <jsp:setProperty name="msg" property="category">
        <jsp:attribute name="value" trim="true">
           <jsp:getProperty name="myBean" property="myProperty" />
        </jsp:attribute>
      </jsp:setProperty>
Here the 'value' attribute of the <jsp:setProperty> action is set using a <jsp:getProperty> action nested in a <jsp:attribute> action element.If you need to define a body for the action element,in addition to attribute values set with <jsp:attribute>, you must define the body with a <jsp:body> element.

    2、using the JSP Expression Language
    <html>
      <head>
        <title>A dose of Dilbert</title>
      </head>
      <body bgcolor="white">
        <h1>A dose of Dilbert</h1>
        <jsp:useBean id="cartoon"
          class="com.ora.jsp.beans.motd.CartoonBean" />
        <img src="images/${cartoon.fileName}">
      </body>
    </html>

四、setting Bean properties
    There two actions can use to set the value:<jsp:setProperty> or <c:set>
    1、Using the <jsp:setProperty> action
       <html>
       <head>
         <title>Messages of the Day</title>
       </head>
       <body bgcolor="white">
         <h1>Messages of the Day</h1>
         <jsp:useBean id="msg"
           class="com.ora.jsp.beans.motd.MixedMessageBean" />
         <h2>Deep Thoughts - by Jack Handey</h2>
         <jsp:setProperty name="msg" property="category"
           value="thoughts" />
         <i>
           <jsp:getProperty name="msg" property="message" />
         </i>
         <h2>Quotes From the Famous and the Unknown</h2>
         <jsp:setProperty name="msg" property="category"
           value="quotes" />
         <i>
           <jsp:getProperty name="msg" property="message" />
         </i>
       </body>
     </html>

    2、Using the JSTL <c:set> action
       <c:set target="${msg}" property="category" value="thoughts" />
       <c:set target="${msg}" property="category" value="quotes" />
    
    The <c:set> is more flexible than <jsp:setProperty>.so if you don't need to be JSP 1.1 compatible,<c:set> is your first choice.




 

 
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载