萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> JSP 程式設計

JSP useBean 標籤實戰:封裝系統屬性查詢 JavaBean

<jsp:useBean> 是 JSP 內建標準動作,用於實例化、定位、存取 JavaBean 組件。本文以查詢 JVM/作業系統屬性為例,完整展示 Bean 類別設計、JSP 頁面引用、屬性存取的標準流程。

一、JavaBean 類別設計

package beanpackage;

public class SystemProperties {
  // 唯讀屬性:只提供 getter,對應 System.getProperty()
  
  public String getRuntimeEnvironmentVersion() { 
    return System.getProperty("java.version"); 
  }
  public String getRuntimeEnvironmentVendor() { 
    return System.getProperty("java.vendor"); 
  }
  public String getVmVersion() { 
    return System.getProperty("java.vm.version"); 
  }
  public String getVmVendor() { 
    return System.getProperty("java.vm.vendor"); 
  }
  public String getVmName() { 
    return System.getProperty("java.vm.name"); 
  }
  public String getVmSpecVersion() { 
    return System.getProperty("java.vm.specification.version"); 
  }
  public String getVmSpecVendor() { 
    return System.getProperty("java.vm.specification.vendor"); 
  }
  public String getVmSpecName() { 
    return System.getProperty("java.vm.specification.name"); 
  }
  public String getRteSpecName() { 
    return System.getProperty("java.specification.name"); 
  }
  public String getRteSpecVersion() { 
    return System.getProperty("java.specification.version"); 
  }
  public String getRteSpecVendor() { 
    return System.getProperty("java.specification.vendor"); 
  }
  public String getOperatingSystemName() { 
    return System.getProperty("os.name"); 
  }
  public String getOperatingSystemVersion() { 
    return System.getProperty("os.version"); 
  }
  public String getOperatingSystemArchitecture() { 
    return System.getProperty("os.arch"); 
  }
}

JavaBean 規範要點

  • Public 類別、無參建構子 (隱式或顯式)
  • 屬性存取:getXxx()/setXxx() (boolean 可用 isXxx())
  • 實作 Serializable (支援分散式 Session/序列化)
  • 無商業邏輯,純資料封裝

二、JSP 頁面使用

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>系統屬性檢視</title>
  <style>
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
    th { background: #f4f4f4; }
  </style>
</head>
<body>
  <h1>JVM 與作業系統屬性</h1>
  
  <!-- 實例化 Bean,id="props" 為頁面變數名 -->
  <jsp:useBean id="props" class="beanpackage.SystemProperties" scope="page" />
  
  <table>
    <tr><th>屬性名稱</th><th>值</th></tr>
    <tr><td>作業系統名稱</td><td><jsp:getProperty name="props" property="operatingSystemName" /></td></tr>
    <tr><td>作業系統版本</td><td><jsp:getProperty name="props" property="operatingSystemVersion" /></td></tr>
    <tr><td>作業系統架構</td><td><jsp:getProperty name="props" property="operatingSystemArchitecture" /></td></tr>
    <tr><td>JDK 版本</td><td><jsp:getProperty name="props" property="runtimeEnvironmentVersion" /></td></tr>
    <tr><td>JDK 廠商</td><td><jsp:getProperty name="props" property="runtimeEnvironmentVendor" /></td></tr>
    <tr><td>JVM 版本</td><td><jsp:getProperty name="props" property="vmVersion" /></td></tr>
    <tr><td>JVM 廠商</td><td><jsp:getProperty name="props" property="vmVendor" /></td></tr>
    <tr><td>JVM 名稱</td><td><jsp:getProperty name="props" property="vmName" /></td></tr>
    <tr><td>JVM 規範版本</td><td><jsp:getProperty name="props" property="vmSpecVersion" /></td></tr>
    <tr><td>JVM 規範廠商</td><td><jsp:getProperty name="props" property="vmSpecVendor" /></td></tr>
    <tr><td>JVM 規範名稱</td><td><jsp:getProperty name="props" property="vmSpecName" /></td></tr>
    <tr><td>Java 規範名稱</td><td><jsp:getProperty name="props" property="rteSpecName" /></td></tr>
    <tr><td>Java 規範版本</td><td><jsp:getProperty name="props" property="rteSpecVersion" /></td></tr>
    <tr><td>Java 規範廠商</td><td><jsp:getProperty name="props" property="rteSpecVendor" /></td></tr>
  </table>
</body>
</html>

三、標籤屬性詳細說明

屬性必要說明
id是頁面範圍內的變數名稱 (對應 pageContext.getAttribute("id"))
class是Bean 完整類別名 (含套件)
scope否page(預設) / request / session / application
type否宣告型別 (介面/父類),配合 class 或 beanName 使用
beanName否java.beans.Beans.instantiate() 可接受的名稱 (含序列化檔)

四、<jsp:getProperty> / <jsp:setProperty>

  • <jsp:getProperty name="beanId" property="propName" />:輸出屬性值 (自動 toString()),屬性需有 getter
  • <jsp:setProperty name="beanId" property="propName" value="..." />:設定屬性值,屬性需有 setter
  • <jsp:setProperty name="beanId" property="*" />:自動對應請求參數同名屬性 (表單綁定神器)

五、現代替代方案

JSP 2.0+ 建議使用 EL (Expression Language) + JSTL:

<!-- 替代 useBean + getProperty -->
<c:set var="props" value="${beanpackage.SystemProperties}" /> <!-- 需配合 Controller 放入 scope -->
${props.operatingSystemName}
${props.runtimeEnvironmentVersion}
...

優點:語法更簡潔、自動處理 null、支援巢狀屬性、與標籤庫無縫整合、關注點分離更徹底。

jsp:useBean 是 JSP 早期連結 Java 組件的標準方式。理解其運作機制 (實例化、作用域、屬性存取) 有助於維護舊系統,但新專案請優先採用 Spring MVC + Thymeleaf/FreeMarker 或 JSP + EL/JSTL 架構。
copyright © 萬盛學電腦網 all rights reserved