1、什麼是JavaBean
- 這個Java類必須具有一個無參的構造函數
- 屬性必須私有化。
- 私有化的屬性必須通過public類型的方法暴露給其它程序,並且方法的命名也必須遵守一定的命名規範。
屬性修改器必須以小寫的set前綴開始,後跟屬性名,且屬性名的第一個字母要改爲大寫,例如,name屬性的修改器名稱爲setName,password屬性的修改器名稱爲setPassword。
屬性訪問器通常以小寫的get前綴開始,後跟屬性名,且屬性名的第一個字母也要改爲大寫,例如,name屬性的訪問器名稱爲getName,password屬性的訪問器名稱爲getPassword。
一個JavaBean的某個屬性也可以只有set方法或get方法,這樣的屬性通常也稱之爲只寫、只讀屬性。
2、在JSP中使用JavaBean
- <jsp:useBean>標籤:用於在JSP頁面中查找或實例化一個JavaBean組件。
- <jsp:setProperty>標籤:用於在JSP頁面中設置一個JavaBean組件的屬性。
- <jsp:getProperty>標籤:用於在JSP頁面中獲取一個JavaBean組件的屬性。
(1)、<jsp:useBean>標籤
- 如果存在則直接返回該JavaBean對象的引用。
- 如果不存在則實例化一個新的JavaBean對象並將它以指定的名稱存儲到指定的域範圍中。
<jsp:useBean id="beanName" class="package.class"
scope="page|request|session|application"/>
- id屬性用於指定JavaBean實例對象的引用名稱和其存儲在域範圍中的名稱。
- class屬性用於指定JavaBean的完整類名(即必須帶有包名)。
- scope屬性用於指定JavaBean實例對象所存儲的域範圍,其取值只能是page、request、session和application等四個值中的一個,其默認值是page。
<jsp:useBean id="currentDate" class="java.util.Date"/>
翻譯成servlet源碼:java.util.Date currentDate = null;
synchronized (_jspx_page_context) {
currentDate = (java.util.Date) _jspx_page_context.getAttribute(
"currentDate", PageContext.PAGE_SCOPE);
if (currentDate == null){
currentDate = new java.util.Date();
_jspx_page_context.setAttribute("currentDate",
currentDate, PageContext.PAGE_SCOPE);
}
}
小注:
<jsp:useBean ...>
Body
</jsp:useBean>
功能:
(2)、<jsp:setProperty>標籤
<jsp:setProperty name="beanName"{
property="propertyName" value="{string | <%= expression %>}" |
property="propertyName" [ param="parameterName" ] |
property= "*"
}/>
- name屬性用於指定JavaBean對象的名稱。
- property屬性用於指定JavaBean實例對象的屬性名。
- value屬性用於指定JavaBean對象的某個屬性的值,value的值可以是字符串,也可以是表達式。爲字符串時,該值會自動轉化爲JavaBean屬性相應的類型,如果value的值是一個表達式,那麼該表達式的計算結果必須與所要設置的JavaBean屬性的類型一致。
- param屬性用於將JavaBean實例對象的某個屬性值設置爲一個請求參數值,該屬性值同樣會自動轉換成要設置的JavaBean屬性的類型。
- property= "*" 各個屬性全部封裝
但是jsp:setProperty標籤對於複雜類型無法自動進行轉換
<!-- jsp:setProperty標籤用所有的請求參數爲bean的屬性賦值 -->
<!-- http://localhost:8080/day09/2.jsp?name=flx&password=123&age=34 -->
<jsp:setProperty name="person" property="*"/>
<%
System.out.println(person.getName());
System.out.println(person.getPassword());
System.out.println(person.getAge());
%>
(3)、<jsp:getProperty>標籤
<jsp:getProperty>標籤用於讀取JavaBean對象的屬性,也就是調用JavaBean對象的getter方法,然後將讀取的屬性值轉換成字符串後插入進輸出的響應正文中。語法:
<jsp:getProperty name="beanInstanceName" property="PropertyName" />
- name屬性用於指定JavaBean實例對象的名稱,其值應與<jsp:useBean>標籤的id屬性值相同。
- property屬性用於指定JavaBean實例對象的屬性名。
- 如果一個JavaBean實例對象的某個屬性的值爲null,那麼,使用<jsp:getProperty>標籤輸出該屬性的結果將是一個內容爲“null”的字符串。一般需要判斷,爲null就不輸出了。
3、JSP開發模式
- 一種是JSP+JavaBean模式(不用)
- 一種是Servlet+JSP+JavaBean模式。
package seu.xinci.pojo;
import java.math.BigDecimal;
/**
* Created by Administrator on 2016/1/31.
*/
public class CalculatorBean {
private double firstNumber;
private double secondNumber;
private char operator='+';
private double result;
public double getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(double firstNumber) {
this.firstNumber = firstNumber;
}
public double getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(double secondNumber) {
this.secondNumber = secondNumber;
}
public char getOperator() {
return operator;
}
public void setOperator(char operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public void calculate(){
switch (this.operator){
case '+':{
this.result = this.firstNumber + this.secondNumber;
break;
}
case '-':{
this.result = this.firstNumber - this.secondNumber;
break;
}
case '*':{
this.result = this.firstNumber * this.secondNumber;
break;
}
case '/':{
if (this.secondNumber==0)
throw new RuntimeException("被除數不能爲0");
this.result = this.firstNumber / this.secondNumber;
this.result = new BigDecimal(this.result).setScale(2,BigDecimal.ROUND_HALF_UP)
.doubleValue();
break;
}
default:
throw new RuntimeException("對不起,傳入的運算符非法!!");
}
}
}
calculator.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>計算器</title>
</head>
<body style="text-align: center;">
<jsp:useBean id="CalculatorBean" class="cn.itcast.CalculatorBean"></jsp:useBean>
<jsp:setProperty name="CalculatorBean" property="*"/>
<%
CalculatorBean.calculate();
%>
<br/><hr> <br/>
計算結果是:
<jsp:getProperty name="CalculatorBean" property="firstNum"/>
<jsp:getProperty name="CalculatorBean" property="operator"/>
<jsp:getProperty name="CalculatorBean" property="secondNum"/>
=
<jsp:getProperty name="CalculatorBean" property="result"/>
<br/><hr> <br/>
<form action="/day09/calculator.jsp" method="post">
<table border="1" width="50%">
<tr>
<td colspan="2">簡單的計算器</td>
</tr>
<tr>
<td>第一個參數</td>
<td>
<input type="text" name="firstNum">
</td>
</tr>
<tr>
<td>運算符</td>
<td>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
</tr>
<tr>
<td>第二個參數</td>
<td>
<input type="text" name="secondNum">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="計算">
</td>
</tr>
</table>
</form>
</body>
</html>
注意:
this.result = new BigDecimal(this.result).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
4、EL表達式快速入門
EL表達式用於獲取數據,在JSP頁面中可使用${標識符}的形式,通知JSP引擎調用pageContext.findAttribute()方法,以標識符爲關鍵字從各個域對象(page、request、session、application)中獲取對象。<%
request.setAttribute("name","aaa");
%>
<!-- pageContext.findAttribute("name") page request session application-->
${name }
(2)、EL表達式中也可以使用${customerBean.address}的形式來訪問JavaBean對象的屬性。
示例:使用EL表達式獲取Bean屬性。
<!-- 在jsp頁面中,使用el表達式可以獲取bean的屬性 -->
<%
Person p = new Person();
p.setAge(12);
request.setAttribute("person",p);
%>
${person.age }
<!-- 在jsp頁面中,使用el表達式可以獲取bean中的。。。。。。。。。的屬性 -->
<%
Person person = new Person();
Address address = new Address();
person.setAddress(address);
request.setAttribute("person",person);
%>
${person.address.name }
<!-- 在jsp頁面中,使用el表達式獲取list集合中指定位置的數據 -->
<%
Person p1 = new Person();
p1.setName("aa111");
Person p2 = new Person();
p2.setName("bb");
List list = new ArrayList();
list.add(p1);
list.add(p2);
request.setAttribute("list",list);
%>
${list[0].name }
(4)、map集合指定數據
<!-- 在jsp頁面中,使用el表達式獲取map集合的數據 -->
<%
Map map = new HashMap();
map.put("a","aaaaxxx");
map.put("b","bbbb");
map.put("c","cccc");
map.put("1","aaaa1111");
request.setAttribute("map",map);
%>
${map.a }
${map["1"] }
<!-- 利用el表達式獲取web應用的名稱 -->
<a href="${pageContext.request.contextPath }/1.jsp">點點</a>
EL表達式也可使用類如${1==1}的形式進行簡單的邏輯判斷。
<!-- 注意:如果訪問bean不存在的屬性,會拋 Property 'username' not found on type cn.itcast.Person -->
<%-- ${person.username }--%>
5、JSTL標籤快速入門
1、導入jstl.jar和standerd.jar這兩個JSTL的jar文件。
2、在JSP頁面中使用<%@ tablib url=“” prifix=“” %>元素導入標籤庫。
<c:foreach var=“” items=“”>
<c:if test=“”>
<!-- 使用jstl+el表達式可以迭代list集合 -->
<%
Person p1 = new Person();
p1.setName("aa111");
Person p2 = new Person();
p2.setName("bb");
List list = new ArrayList();
list.add(p1);
list.add(p2);
request.setAttribute("list",list);
%>
<c:forEach var="person" items="${list}">
${person.name }<br/>
</c:forEach>
(2)、map的迭代:
<!-- 使用jstl+el表達式可以迭代map集合 -->
<%
Map map = new HashMap();
map.put("a","aaaaxxx");
map.put("b","bbbb");
map.put("c","cccc");
map.put("1","aaaa1111");
request.setAttribute("map",map);
%>
<br/>
<%-- Set<Map.Entry> set = map.entrySet()--%>
<c:forEach var="me" items="${map}">
${me.key } = ${me.value }<br/>
</c:forEach>
(3)、c:if的使用:
<c:if test="${user!=null}">
歡迎您:${user.uesrname }
</c:if>
<c:if test="${user==null}">
用戶名:<input type="text" name="username">
密碼:<input type="password" name="password">
<input type="submit" value="登陸">
</c:if>