JAVA入門[12]-JavaBean

來源:互聯網
上載者:User

標籤:value   count   pack   void   main   入門   tle   ...   submit   

一.什麼是JavaBean

JavaBean是特殊的Java類,使用Java語言書寫,並且遵守規範:

  • 提供一個預設的無參建構函式。
  • 需要被序列化並且實現了Serializable介面。
  • 可能有一系列可讀寫屬性。
  • 可能有一系列的"getter"或"setter"方法。
二.定義JavaBean
package com.cathy.domain;public class Category implements java.io.Serializable{    public Category(){}    private int cateId;    private String cateName;    public int getCateId() {        return cateId;    }    public void setCateId(int cateId) {        this.cateId = cateId;    }    public String getCateName() {        return cateName;    }    public void setCateName(String cateName) {        this.cateName = cateName;    }}

  

三、訪問JavaBean

1. <jsp:useBean> 標籤可以在JSP中聲明一個JavaBean,文法格式如下:

<jsp:useBean id="bean 的名字" scope="bean 的範圍" />

其中scope的值可以是page,request,session或application

2.設定和擷取JavaBean屬性

在 <jsp:useBean> 標籤主體中使用 <jsp:getProperty/> 標籤來調用 getter 方法擷取屬性,使用 <jsp:setProperty/> 標籤調用 setter 方法設定屬性。文法格式:

<jsp:useBean id="id" class="bean 類" scope="bean 範圍"> <jsp:setProperty name="bean 的 id" property="屬性名稱"value="value"/><jsp:getProperty name="bean 的 id" property="屬性名稱"/>   ........... </jsp:useBean> 

  

其中name屬性指的是Bean的id屬性,property屬性指的是想要調用的getter或setter方法

四、樣本

1.樣本:在當前jsp版面設定和擷取javabean屬性

<jsp:useBean id="category" class="com.cathy.domain.Category">    <jsp:setProperty name="category" property="cateId" value="2"></jsp:setProperty>    <jsp:setProperty name="category" property="cateName" value="女裝"></jsp:setProperty></jsp:useBean><div>    id:<jsp:getProperty name="category" property="cateId"></jsp:getProperty></div><div>    name:<jsp:getProperty name="category" property="cateName"></jsp:getProperty></div>

  

2.樣本:在edit.jsp 頁面form表單提交資訊,在detail.jsp頁面中顯示。

edit.jsp

<form action="/category/detail" method="post">    商品id:<input type="number" name="cateId">    商品名稱:<input type="text" name="cateName">    <input type="submit" value="提交"></form>

  

detail.jsp

如果表單中的屬性名稱和JavaBean中屬性對應,那麼可以簡化jsp:setProperty寫法,直接property="*"

<jsp:useBean id="category" class="com.cathy.domain.Category">    <jsp:setProperty name="category" property="*"></jsp:setProperty></jsp:useBean><table>    <tr>        <td>id:</td><td><jsp:getProperty name="category" property="cateId"></jsp:getProperty></td>    </tr>    <tr><td>名稱:</td><td><jsp:getProperty name="category" property="cateName"></jsp:getProperty></td></tr></table>

  

問題:通過表單提交中文資料時遇到亂碼問題:

<%request.setCharacterEncoding("UTF-8");%>

  

效果:

3.樣本:JavaBean實現訪問計數

首先建立計數JavaBean:

public class Counter {    private  int total=0;    public int getTotal() {        return total++;    }}

  

在index.jsp和edit.jsp檔案中調用該JavaBean,注意將scope設定為application。

<jsp:useBean id="counter" class="com.cathy.domain.Counter" scope="application"></jsp:useBean><div>    訪問計數:<jsp:getProperty name="counter" property="total"></jsp:getProperty></div>

  

重新整理這兩個頁面時,都能實現計數器累加。

JAVA入門[12]-JavaBean

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.