JSP JavaBean

JavaBean 鏄壒娈婄殑 Java 绫伙紝浣跨敤 Java 璇█涔﹀啓锛屽苟涓旈伒瀹 JavaBean API 瑙勮寖銆

鎺ヤ笅鏉ョ粰鍑虹殑鏄 JavaBean 涓庡叾瀹 Java 绫荤浉姣旇岃█鐙竴鏃犱簩鐨勭壒寰侊細

  • 一分快三靠谱吗—官方网址22270.COM鎻愪緵涓涓粯璁ょ殑鏃犲弬鏋勯犲嚱鏁般
  • 闇瑕佽搴忓垪鍖栧苟涓斿疄鐜颁簡 Serializable 鎺ュ彛銆
  • 鍙兘鏈変竴绯诲垪鍙鍐欏睘鎬с
  • 鍙兘鏈変竴绯诲垪鐨 getter 鎴 setter 鏂规硶銆

JavaBean 灞炴

一分快三靠谱吗—官方网址22270.COM涓涓 JavaBean 瀵硅薄鐨勫睘鎬у簲璇ユ槸鍙闂殑銆一分快三靠谱吗—官方网址22270.COM杩欎釜灞炴у彲浠ユ槸浠绘剰鍚堟硶鐨 Java 鏁版嵁绫诲瀷锛屽寘鎷嚜瀹氫箟 Java 绫汇

涓涓 JavaBean 瀵硅薄鐨勫睘鎬у彲浠ユ槸鍙鍐欙紝鎴栧彧璇伙紝鎴栧彧鍐欍侸avaBean 瀵硅薄鐨勫睘鎬ч氳繃 JavaBean 瀹炵幇绫讳腑鎻愪緵鐨勪袱涓柟娉曟潵璁块棶锛

鏂规硶 鎻忚堪
getPropertyName() 涓句緥鏉ヨ锛屽鏋滃睘鎬х殑鍚嶇О涓 myName锛岄偅涔堣繖涓柟娉曠殑鍚嶅瓧灏辫鍐欐垚 getMyName() 鏉ヨ鍙栬繖涓睘鎬с傝繖涓柟娉曚篃绉颁负璁块棶鍣ㄣ
setPropertyName() 涓句緥鏉ヨ锛屽鏋滃睘鎬х殑鍚嶇О涓 myName锛岄偅涔堣繖涓柟娉曠殑鍚嶅瓧灏辫鍐欐垚 setMyName()鏉ュ啓鍏ヨ繖涓睘鎬с傝繖涓柟娉曚篃绉颁负鍐欏叆鍣ㄣ

一分快三靠谱吗—官方网址22270.COM涓涓彧璇荤殑灞炴у彧鎻愪緵 getPropertyName() 鏂规硶锛屼竴涓彧鍐欑殑灞炴у彧鎻愪緵 setPropertyName() 鏂规硶銆


JavaBean 绋嬪簭绀轰緥

杩欐槸 StudentBean.java 鏂囦欢锛

package com.runoob;

public class StudentsBean implements java.io.Serializable
{
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean() {
   }
   public String getFirstName(){
      return firstName;
   }
   public String getLastName(){
      return lastName;
   }
   public int getAge(){
      return age;
   }

   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(int age) {
      this.age = age;
   }
}

缂栬瘧 StudentBean.java 鏂囦欢(鏈鍚庝竴涓疄渚嬩細鐢ㄥ埌):

$ javac StudentsBean.java

缂栬瘧鍚庤幏寰 StudentBean.class 鏂囦欢锛屽皢鍏舵嫹璐濆埌 <JSP 椤圭洰>/WebContent/WEB-INF/classes/com/runoob锛屽涓嬪浘鎵绀:


璁块棶JavaBean

<jsp:useBean> 鏍囩鍙互鍦 JSP 涓0鏄庝竴涓 JavaBean锛岀劧鍚庝娇鐢ㄣ一分快三靠谱吗—官方网址22270.COM澹版槑鍚庯紝JavaBean 瀵硅薄灏辨垚浜嗚剼鏈彉閲忥紝鍙互閫氳繃鑴氭湰鍏冪礌鎴栧叾浠栬嚜瀹氫箟鏍囩鏉ヨ闂<jsp:useBean> 鏍囩鐨勮娉曟牸寮忓涓嬶細

<jsp:useBean id="bean 鐨勫悕瀛" scope="bean 鐨勪綔鐢ㄥ煙" typeSpec/>

鍏朵腑锛屾牴鎹叿浣撴儏鍐碉紝scope 鐨勫煎彲浠ユ槸 page锛宺equest锛宻ession 鎴 application銆俰d鍊煎彲浠绘剰鍙涓嶅拰鍚屼竴 JSP 鏂囦欢涓叾瀹 <jsp:useBean> 涓 id 鍊间竴鏍峰氨琛屼簡銆

鎺ヤ笅鏉ョ粰鍑虹殑鏄 <jsp:useBean> 鏍囩鐨勪竴涓畝鍗曠殑鐢ㄦ硶锛

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>useBean 瀹炰緥</title>
</head>
<body>

<jsp:useBean id="date" class="java.util.Date" /> 
<p>鏃ユ湡涓猴細<%= date %>

</body>
</html>

瀹冨皢浼氫骇鐢熷涓嬬粨鏋滐細

鏃ユ湡涓猴細Tue Jun 28 15:22:24 CST 2016

璁块棶 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>

一分快三靠谱吗—官方网址22270.COMname灞炴ф寚鐨勬槸Bean鐨刬d灞炴с俻roperty灞炴ф寚鐨勬槸鎯宠璋冪敤鐨刧etter鎴杝etter鏂规硶銆

鎺ヤ笅鏉ョ粰鍑轰娇鐢ㄤ互涓婅娉曡繘琛屽睘鎬ц闂殑涓涓畝鍗曚緥瀛愶細

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>get 鍜 set 灞炴у疄渚</title>
</head>
<body>

<jsp:useBean id="students" 
                    class="com.runoob.StudentsBean"> 
   <jsp:setProperty name="students" property="firstName"
                    value="灏忓己"/>
   <jsp:setProperty name="students" property="lastName" 
                    value="鐜"/>
   <jsp:setProperty name="students" property="age"
                    value="10"/>
</jsp:useBean>

<p>瀛︾敓鍚嶅瓧: 
   <jsp:getProperty name="students" property="firstName"/>
</p>
<p>瀛︾敓濮撴皬: 
   <jsp:getProperty name="students" property="lastName"/>
</p>
<p>瀛︾敓骞撮緞: 
   <jsp:getProperty name="students" property="age"/>
</p>

</body>
</html>

璁块棶浠ヤ笂 JSP锛岃繍琛岀粨鏋滃涓嬶細

瀛︾敓鍚嶅瓧: 灏忓己

瀛︾敓濮撴皬: 鐜

瀛︾敓骞撮緞: 10