jsp標簽jsp:setProperty用法


jsp標簽jsp:setProperty用法

 
來源:http://blog.csdn.net/wanghaishanren/article/details/2047400

 

<jsp:setProperty>用來設置已經實例化的Bean對象的屬性

第一種形式:

<jps:setProperty name = "JavaBean實例名"  property = "*"/>

該形式是設置Bean 屬性的快捷方式.在Bean 中屬性的名字,類型必須和request對象中的參數名稱相匹配。由於表單中傳過來的數據類型都是String 類型的,Jsp內在機制會把這些參數轉化成Bean屬性對應的類型。

property = "*"表示所有名字和Bean屬性名字匹配的請求參數都將被傳遞給相應的屬性set方法。  

第二種形式:

<jsp:setProperty name = "JavaBean實例名" property = "JavaBean屬性名" />

使 用request對象中的一個參數值來指定Bean中的一個屬性值。在這個語法中,property指定Bean 的屬性名,而且Bean 屬性和 request參數的名字應相同。也就是說,如果在Bean 中有setUserName(String userName)方法,那 么,propertyName的值就是"userName".這種形式靈活性較強,可以有選擇的對Bean中的屬性賦值

第三種形式:

<jsp:setProperty name = "JavaBean實例名" property = "JavaBean屬性名" value = "BeanValue"/>

value 用來指定Bean屬性的值。字符串數據會在目標類中通過標准的valueOf方法自動轉換成數字、boolean、Boolean、byte、Byte、 char、Character。例如,boolean和Boolean類型的屬性值(比如“true”)通過Boolean.valueOf轉換,int 和Integer類型的屬性值(比如“42”)通過Integer.valueOf轉換。 

第四種形式:

<jsp:setProperty name = "JavaBean實例名" 

     property = "propertyName" param = "request對象中的參數名"

/>

  param指定用哪個請求參數作為Bean屬性的值。Bean 屬性和request參數的名字可以不同。如果當前請求沒有參數,則什么事情也不做,系統不會把null傳遞給Bean屬性的set方法。因此,你可以讓Bean自己提供默認屬性值,只有當請求參數明確指定了新值時才修改默認屬性值。 

例如,下面的代碼片斷表示:如果存在numItems請求參數的話,把numberOfItems屬性的值設置為請求參數numItems的值;否則什么也不做。 

 

<jsp:setProperty name="orderBean" property="numberOfItems" param="numItems" /> 

下面是一個簡單的例子:

 

//sampleBean.java

 

package MyJavaBeanPackage;

 

public class SampleBean2

{

 private String id;

 private String age;

 private String name;

 private String sex;

 private String addr;

public SampleBean2()

 {

 }

 public String getName()

 {

  return name;

 }

 public void setName(String name)

 {

  this.name = name;

 }

 public String getSex()

 {

  return sex;

 }

 public void setSex(String sex)

 {

  this.sex = sex;

 }

 

 public void setId(String id)

 {

  this.id = id;

 }

 public void setAge(String age)

 {

  this.age = age;

 }

 public String getId()

 {

  return id;

 }

 public String getAge()

 {

  return age;

 }

 public String getAddr()

 {

  return addr;

 }

 public void setAddr(String addr)

 {

  this.addr = addr;

 }

}

 

//SampleBean.html

<html>

  <head>

    <title>使用<jsp:getProperty>和<jsp:setProperty>標記</title>

  </head>

  

  <body>

   <form name = "form1" action = "Sample2.jsp" method = "post">

    <p align = "center">編號

     <input type = "text" name = "id">

    </p>

    <p align = "center">姓名

     <input type = "text" name = "name">

    </p>

    <p align = "center">性別

     <select name = "TheSex" id = "sex">

      <option value = "男" selected>男</option>

      <option value = "女">女</option>

       </select>

     </p>

     <p align = "center">年齡

      <input type = "text" name = "TheAge">

     </p>

     <p align = "center">

      <input type = "submit" value = "提交">

      <input type = "reset" value = "重置">

     </p>

    </form>

   </body>

 </html>

     

 

 

   //SampleBean.jsp

<%@ page

 language="java" 

 contentType = "text/html;charset = GBK"

%>

 

<jsp:useBean id = "sampleBean2" class = "MyJavaBeanPackage.SampleBean2"/> 

<html>

 <head>

  <title>在JavaBean中存放數據</title>

 </head>

 <body>

  <%--方法一

   <jsp:setProperty name = "sampleBean2" property = "*" />

  --%>

  <%-- 方法二 --%>

  <jsp:setProperty name = "sampleBean2" property = "id"/>

  <jsp:setProperty name = "sampleBean2" property = "name"/>

  <%--方法三 --%>

  <jsp:setProperty name = "sampleBean2" property = "addr" value = "洛陽師范學院"/>

  <%-- 方法四 bean屬性的名字可以與request參數不同--%>

  <jsp:setProperty name = "sampleBean2" property = "sex" param = "TheSex"/>

  <jsp:setProperty name = "sampleBean2" property = "age" param = "TheAge"/>

  <center>

   編號:<jsp:getProperty name = "sampleBean2" property = "id"/><br>

   姓名:<jsp:getProperty name = "sampleBean2" property = "name"/><br>

   性別:<jsp:getProperty name = "sampleBean2" property = "sex"/><br> 

   年齡:<jsp:getProperty name = "sampleBean2" property = "age"/><br>

   住址:<jsp:getProperty name = "sampleBean2" property = "addr"/><br>

  </center>

 </body>

</html>   

 

 

 

//參考文獻:

1<<從零開始——JSP動態網頁制作基礎培訓教程-源代碼>>

2

http://hi.baidu.com/ffcheng1987/blog/item/44c71755925761c7b645ae2c.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM