Các loại thuộc tính.

Một phần của tài liệu JSP Technology.doc (Trang 29 - 33)

I. Khái niệm và các quy ước của Bean 1 Khái niệm.

b)Các loại thuộc tính.

Chúng ta hãy xem các ví dụ sau:

- Thuộc tính mang những giá trị đơn. Bean sau đây lấy thời gian của hệ thống.

Code 15: Java file

package com.legiang.bean; import java .util.*;

public class CurrentTimeBean { /*Data members*/

private int hours; private int minutes; /* Methods */

Code 15: Java file (tiếp theo)

public CurrentTimeBean() { Date now = new Date(); this.hours = now.getHours(); this.minutes = now.getMinutes(); }

public int getHours() { return hours;

}

public int getMinutes() { return minutes(); }

Hải Code 16: JSP file <jsp:useBean id="time" class="com.legiang.bean.CurrentTimeBean"/> <html> <body>

It is now <jsp:getProperty name="time" property="minutes"/>

minutes past the hour. </body>

</html>

- Thuộc tính mang giá trị boolean.

Code 17: Java file

package com.legiang.bean; public class LogicBean { /*Data members*/

private boolean authorized; /*Methods*/

public LogicBean() { this.authorized = false; }

public void setAuthorized(boolean authorized) { this.authorized = authorized;

}

public boolean isAuthorized() {

Code 17: Java file (tiếp theo)

return authorized; } } Code 18: JSP file <jsp:useBean id="logic" class="com.legiang.bean.LogicBean"/> <html> <body> Do you authorize?<br/> Answer:<jsp:getProperty name="logic" property="authorized"/>

Đồ án tốt nghiệp Gvhd: Ts.Nguyễn Thúc Hải

</body> </html>

- Thuộc tính mang giá trị mảng.

Ví dụ này sẽ xây dựng một component mà cĩ thể thực hiện các tính tốn tĩnh trên một dãy số.

Code 19: Java file

package com.legiang.bean; import java.util.*; (adsbygoogle = window.adsbygoogle || []).push({});

public class StatBean { /*Data members*/

private double[] numbers; /*Methods*/

public StatBean() {

numbers = new double[0]; }

public double getAverage() { double sum = this.getSum(); if (sum == 0)

return 0; else

return sum/numbers.length; }

public double getSum() {

Code 19: Java file (tiếp theo)

double sum = 0;

for (int i=0; i < numbers.length; i++) sum += numbers[i];

return sum; }

public double[] getNumbers() { return numbers;

}

public double getNumbers(int index) { return numbers[index];

}

public void setNumbers(double[] numbers) { this.numbers = numbers;

Hải

}

public void setNumbers(int index, double value) { numbers[index] = value;

}

public int getNumbersSize() { return numbers.length; }

}

Code 20: JSP file

<jsp:useBean id="stat" class="com.lg.bean.StatBean"> <% double[] mynums = {100, 250, 150, 50, 450}; stat.setNumbers(mynums); %> </jsp:useBean> <html> <body> The average of <%

double[] numbers = stat.getNumbers(); for (int i=0; i < numbers.length; i++) { if (i != numbers.length)

out.print(numbers[i] + ","); else

Code 20: Java file (tiếp theo)

out.println("" + numbers[i]); }

%> (adsbygoogle = window.adsbygoogle || []).push({});

is equal to <jsp:getProperty name="stat" property="average" />

</body> </html>

Ngồi các loại thuộc tính trên, Java cịn cung cấp một số thuộc tính nâng cao khác dùng cho các Bean đồ hoạ.

Đồ án tốt nghiệp Gvhd: Ts.Nguyễn Thúc Hải

Một phần của tài liệu JSP Technology.doc (Trang 29 - 33)