標籤和標籤 筆記

來源:互聯網
上載者:User

<jsp:useBean>標籤用來在JSP頁面中建立一個Bean執行個體。並指定它的名字及作用範圍。它保證對象在標籤指定的範圍內可以使用。

文法如下:

<jsp:useBean id="id"scope="page|request|session|application" typeSpec/>

id為大小敏感的名字,用來表示這個執行個體;

scope表示此對象可以使用的範圍;

typeSpec可以為以下四者之一:

·   class="className" Bean的類路徑和類名。這個class不能是抽象的,必須有一個共用的,沒有參數的構造器。

·   class="className" type="typeName"執行個體化一個Bean,同時還可以指定Bean的類型。

·   beanName="beanName" type="typeName"

·   type="typeName"

例:

Bean,定義Bean,定義了userName,password,age三個變數及三個變數的get/set方法。

package com.jspdev.ch3;public class TestBean{   public String userName;   public String password;   public int age;      public void setUserName(String name){      this.userName=name;   }   public void setPassword(String password){      this.password=password;   }   public String getUserName(){      return this.userName;   }   public String getPassword(){      return password;   }   public int getAge(){      return this.age;   }   public void setAge(int age){      this.age=age;   }}

表單,定義了表單中的參數,注意參數名和Bean中定義的變數需要一致(包括大小寫)。

<html><body>使用者資訊註冊:<br><hr><form method="get"action="register.jsp"><table><tr><td>姓名:<inputname="userName"type="text"></td></tr><tr><td>密碼:<inputname="password"type="password"></td></tr><tr><td>年齡:<inputname="age"type="text"></td></tr><tr><td><inputtype=submitvalue="submit"></td></tr></table></form></body></html> 

JSP,<jsp:useBean>標籤指了在本頁面中使用JavaBean,此JavaBean的類為com.jspdev.ch3.TestBean,ID為"user"。

使用方法<jsp:setProperty>將request中的參數與JavaBean中的參數對應起來。name為之前用jsp:useBean引入的名稱即,"user"。

使用user.getUserName()方法來獲得使用者名稱資訊,這的"user"實際上指向一個JavaBean對象。也可以使用

<jsp:getPropertyname="user" property="userName"/>來獲得JavaBean的屬性。

<%@ pagecontentType="text/html;charset=gb2312"%><jsp:useBean id="user"scope="page"class="com.jspdev.ch3.TestBean"/><jsp:setPropertyname="user" property="*"/><html><body>註冊成功:<br><hr>使用bean屬性方法:<br>使用者名稱:<%=user.getUserName()%><br>密碼:<%=user.getPassword()%><br>年齡:<%=user.getAge()%><br><hr>使用getProperty:<br>使用者名稱:<jsp:getProperty name="user"property="userName"/><br>密碼:<jsp:getProperty name="user"property="password"/><br>年齡:<jsp:getProperty name="user"property="age"/><br></body></html>

<jsp:setProperty>標籤,用來和useBean一起協作,用來設定Bean的簡單屬性和索引屬性。

文法如下:

<jsp:setPropertyname="beanName" propertyDetails />

name="beanName"為必選屬性,其值為Bean的名字,在這之前用jsp:userBean引入的名稱。

proertyDetails可以有四種不同的方法。

·   property="*"設定Bean捷徑,在Bean中,屬性的名字、類型必須和request對象中的參數名稱相匹配。

·   property="propertyName"param="parameterName" 將Bean中的變數和request中的參數對應起來。property為Bean的屬性名稱,param為request中的參數名。

·   property="propertyName"      

·   property="propertyName" value="propertyValue"  直接設定Bean的值。

 

舉例說明,當使用property="*"時,可以如下操作:

<html><body>使用者資訊註冊:<br><hr><form method="get"action="register.jsp"><table><tr><td>姓名:<inputname="userName"type="text"></td></tr><tr><td>密碼:<inputname="password"type="password"></td></tr><tr><td>年齡:<inputname="age"type="text"></td></tr><tr><td><inputtype=submitvalue="submit"></td></tr></table></form></body></html>

對以上HTML語言,在Bean已經寫好前提下,可以使用如下的代碼:

<jsp:useBean id="user"scope="page"class="com.jspdev.ch3.TestBean"/><jsp:setPropertyname="user" property="*"/><html><body>註冊成功:<br><hr>使用bean屬性方法:<br>使用者名稱:<%=user.getUserName()%><br>密碼:<%=user.getPassword()%><br>年齡:<%=user.getAge()%><br><hr>使用getProperty:<br>使用者名稱:<jsp:getProperty name="user"property="userName"/><br>密碼:<jsp:getProperty name="user"property="password"/><br>年齡:<jsp:getProperty name="user"property="age"/><br>

*<jsp:getProperty>標籤,用來訪問Bean的屬性。<jsp:getPropertyname="user"property="userName"/>中,name="user",是使用<jsp:useBean>所擷取的Bean;property="userName",是Bean中的變數名,也可以理解為是HTML中的<inputname="password" type="password">

原文來自:http://blog.sina.com.cn/s/blog_66c2c28e01017113.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.