By: Henry in Java Beans Tutorials on 2007年09月23日 [フレーム]
EL in itself is not very useful when creating web applications. In this tutorial, you'll focus on how to use the EL to read values from JavaBeans to display within a JSP page. In previous incarnations of the JSP specification, you would have had to use code such as the following to read values from a JavaBean:<jsp:getProperty name="myBean" property="name" />
An alternative (and more common) method is to use a scriptlet such as the following:
<%= myBean.getName()%>
The use of scriptlets does not represent good practice in JSP development. This may make you ask the question, "If I can use the <getProperty> standard action, why does anyone use scriptlets?" The answer to this question is simple: We developers are lazy! The scriptlet option represents less code and is a lot quicker to type!
To get around this problem, the EL provides a nice way to access the properties of a JavaBean that is in scope within a page, request, session, or application. To achieve the same as the previous code sample, you can use the following expression:
${myBean.name}
This is a nice neat way to access properties; there are no nasty brackets or any other Java like syntax present. This brings us to another core feature of the EL: the concept of named variables. The EL provides a generalized mechanism for resolving variable names into objects. This mechanism has the same behavior as the pageContext.findAttribute() method of the PageContext object. Take the following, for example:
${product}
This expression will look for the attribute named product by searching the page, request, session, and application scopes, in that order, and will print its value. This works regardless of how the object was placed into the particular scope. That is, one JSP page could put an attribute into request, session, or application scope, and another JSP page could access the attribute simply by using an EL statement that uses the name of the attribute. If the attribute is not found, null will be returned. This method is also used to resolve the implicit objects that we"ll talk about in the next section.
Listing below is a JSP page that uses EL to access JavaBeans.
simpleBean.jsp
<jsp:useBean id="person" class="com.apress.projsp.Person" scope="request">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
<html>
<head>
<title>EL and Simple JavaBeans</title>
<style>
body, td {font-family:verdana;font-size:10pt;}
</style>
<head>
<body>
<h2>EL and Simple JavaBeans</h2>
<table border="1">
<tr>
<td>${person.name}</td>
<td>${person.age}</td>
<td> </td>
</tr>
<tr>
<form action="simpleBean.jsp" method="post">
<td><input type="text" name="name"></td>
<td><input type="text" name="age"></td>
<td><input type="submit"></td>
</form>
<tr>
</table>
</body>
</html>
The JSP page above uses a JavaBean of type com.apress.projsp.Person. That JavaBean is shown in Listing below.
//Person.java
package com.apress.projsp;
public class Person {
private String name;
private int age;
public Person() {
setName("A N Other");
setAge(21);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
Use the following steps to deploy the JSP page and JavaBean into a web container:
You should see something similar to the page shown below.
EL can be used to access JavaBeans and the
properties of JavaBeans.
The JSP page in Listing 3-6 creates a JavaBean of type
com.apress.projsp.Person with an
id of person, and sets its properties to the values of
parameters in the HTTP request with the
same name as the properties. This is achieved with the following
code:
<jsp:useBean id="person" class="com.apress.projsp.Person" scope="request">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
The <jsp:setProperty> tag has various syntaxes. When you use property="*", that tells the page implementation class to find each request parameter with the same name as a JavaBean property, and to set the JavaBean property with the value of the request parameter. The JSP page accesses the object via the id given to it in the previous <useBean> tag, in this case, person. The page then displays the values of the properties of the Person JavaBean in a table; this is achieved by the following code:
<tr>
<td>${person.name}<td>
<td>${person.age}</td>
<td> </td>
</tr>
The id is used to access the JavaBean that you declared with the previous <useBean> tag. In this example, the object was created and accessed within the same page. However, as we noted earlier, the object could have been created from any page, and still accessed in the simpleBean.jsp page.
It's worth noting that you could have used the following code to access the properties of our JavaBean:
<tr>
<td>${person["name"]}</td>
<td>${person["age"]}</td>
<td> </td>
</tr>
Try changing the values in the form and clicking Submit Query.
You should see your new
values in the table. Now that you"ve seen a very simple use of
JavaBeans and the EL, you'll look
at a more complex use of the two technologies.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java Beans )
A sample that shows Java Beans, Servlets and JSP working together
Java Beans and the Expression Language
Design Patterns for Properties in a Java Bean
Creating a JavaBean to Connect with Google API
What is EJB server and what are EJB Components?
Latest Articles (in Java Beans)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate