Академический Документы
Профессиональный Документы
Культура Документы
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. organization. Contact hall@coreservlets.com for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
Understanding the benefits of beans
We will use standalone beans here. Later sections will cover beans with MVC and the JSP expression language.
Creating beans Installing bean classes on your server Accessing bean properties Explicitly setting bean properties Automatically setting bean properties from request parameters Sharing beans among multiple servlets and JSP pages
4
Persistent values should be accessed through methods called getXxx and setXxx
If class has method getTitle that returns a String, class is said to have a String property named title Boolean properties may use isXxx instead of getXxx It is the name of the method, not instance var that matters!
executive
executive
ZIP
Note 1: property name does not exist anywhere in your code. It is just a shortcut for the method name. Note 2: property name is derived only from method name. Instance variable name is irrelevant. 8
with
private double speed;
Note: in Eclipse, after you create instance variable, if you R-click and choose Source, it gives you option to generate getters and setters for you.
public d bl getSpeed() { bli double d() return(speed); } public void setSpeed(double newSpeed) { bli id tS d(d bl S d) speed = newSpeed; }
Y You should do this in all your J h ld d thi i ll Java code d anyhow. Why?
9
If users of your class accessed the fields directly, then they would each be responsible for checking constraints.
10
11
If users of your class accessed the fields directly, then they would each be responsible for executing side effects. Too much work and runs huge risk of having display inconsistent from actual values.
12
jsp:setProperty
This element modifies a bean property (i.e., calls a setBlah method). It is normally used as follows:
<jsp:setProperty name="beanName" property="propertyName" p p y value="propertyValue" />
jsp:getProperty
This element reads and outputs the value of a bean property. property It is used as follows:
<jsp:getProperty name="beanName" property="propertyName" />
13
JSP Page
JSP page instantiates a bean
<jsp:useBean id="myBean" class=""/> j y
Purpose
All instantiation of Java classes without explicit Java Allow i i i fJ l ih li i J programming (XML-compatible syntax)
Notes
Simple interpretation: <jsp:useBean id="book1" class="coreservlets.Book" /> can be thought of as equivalent to the scriptlet <% coreservlets.Book book1 = new coreservlets.Book(); %>
Purpose
Allow setting of bean properties (i.e., calls to setXxx g p p ( methods) without explicit Java programming
Notes
<jsp:setProperty name="book1" j P "b k1" property="title" value="Core Servlets and JavaServer Pages" /> <% book1.setTitle("Core Servlets and JavaServer Pages"); %>
16
Purpose
All access to bean properties (i.e., calls to getXxx Allow b i (i ll X methods) without explicit Java programming
Notes
<jsp:getProperty name="book1" property="title" /> is equivalent to the following JSP expression q g p <%= book1.getTitle() %>
17
Example: StringBean
package coreservlets; public class StringBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; hi }
20
21
22
24
25
27
This is extremely convenient for making "form beans" -- objects whose properties are fill d in from a form submission. filled i f f b i i
You can even divide the process up across multiple forms, where each submission fills in part of the object.
28
Sharing Beans
You can use the scope attribute to specify additional places where bean is stored ddi i l l h b i d
Still also bound to local variable in _jspService <jsp:useBean id=" " class=" " id="" class="" scope="" />
Lets multiple servlets or JSP pages p p g share data Also permits conditional bean creation
Creates new object only if it can't find existing one
29
page2.jsp
<jsp:useBean id="foo" class="" scope="application"/> jp p pp <jsp:getProperty name="foo" property="message"/>
Possible scenario 1
J goes t page 2 ( t t is "Default Message") Joe to (output i "D f lt M ") Jane goes to page 1 (output is "Hello")
Possible scenario 2
Joe goes to page 1 (output is "Hello") Jane goes to page 2 (output is "Hello")
30
32
Important:
Use different names (i.e., id in jsp:useBean) for different (i e beans
33
34
35
37
38
40
41
42
43
45
46
47
48
50
51
52
53
}
55
57
Summary
Benefits of jsp:useBean
Hides the Java syntax Makes it easier to associate request parameters with Java objects (bean properties) Simplifies sharing objects among multiple requests or servlets/JSPs
j jsp:useBean B
Creates or accesses a bean
jsp:setProperty
Sets bean property (i.e. passes value to setXxx)
You usually use p p y y property="*" to p pass in request params q p
jsp:getProperty
58
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
59
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.