In this section , we will discuss about Standard actions - 'jsp:setProperty' & 'jsp:getProperty' with an example.
In this section , we will discuss about Standard actions - 'jsp:setProperty' & 'jsp:getProperty' with an example.<jsp:setProperty>
It sets a property value or values in a Bean only if a new object was instantiated, not if an existing one was found.
Syntax:
<jsp:setProperty |
Example :
<jsp:setProperty name="Ibean" property="*" />
<jsp:setProperty name="Ibean" property="user" />
<jsp:setProperty name="Ibean" property="user" value="Ankit" />
Attributes :
The name of an instance of a Bean that has already been
created or located with a <jsp:useBean>
element. The value of
name
must match the value of id
in <jsp:useBean>
.
The <jsp:useBean>
element must appear before <jsp:seProperty>
in the JSP file.
The value of "*"
means that all request parameters whose names
match bean property names will be passed to the appropriate setter methods.
Sets one Bean property to the value of one request parameter. In the syntax,
property
specifies the name of the Bean property and param
specifies the name of the request parameter by which data is being sent from the
client to the server.
If the Bean property and the request parameter have
different names, you must specify both property
and param
.
If they have the same name, you can specify property
and omit
param
. If a parameter has an empty or null value, the corresponding Bean
property is not set.
This optional attribute specifies the value for the property. String values
are automatically converted to numbers, boolean
, Boolean
,
byte
, Byte
, char
, and Character
via the standard valueOf
method in the target or wrapper class. For
example, a value of "true"
for a boolean
or
Boolean
property will be converted via Boolean.valueOf
, and
a value of "42"
for an int
or Integer
property will be converted via Integer.valueOf
.
This element retrieves the value of a bean property, converts it to a string, and inserts it into the output. The two required attributes are:
Syntax :
<jsp:getProperty name="beanInstanceName" property="propertyName" /> |
Limitations of <jsp:getProperty>
:
<jsp:getProperty>
to retrieve the values of
an indexed property. <jsp:getProperty>
with JavaBeans components,
but not with enterprise beans. As alternatives, you can write a JSP page
that retrieves values from a Bean that in turn retrieves values from an
enterprise bean, or you can write a custom tag that retrieves values from an
enterprise bean directly. String
parameter :
<HTML> <head> <TITLE>JavaBeans in JSP</TITLE> <meta http-equiv="Content-Language" content="en-us"> <style type="text/css"> .style1 { border: 2px solid #0000FF; background-color: #CCFFFF; } </style> </head> <BODY> <table class="style1" style="float: CENTER" align="center"> <tr> <td style="width: 430px; height: 38px"> <h3><strong>EXAMPLE OF JSP:USEBEAN STANDARD ACTION</strong></h3> </td> </tr> </table> <P> <jsp:useBean id="test" class="foo.usebeanexample" /> <jsp:setProperty name="test" property="message" value="Hello ANKIT" /> <center><H1>Message: <jsp:getProperty name="test" property="message" /> </H1></center> </BODY> </HTML> |
Given below is the code for the bean used in the "usebean.jsp" :
usebeanexample.java
package foo; public class usebeanexample { private String message = "No message specified"; public String getMessage() { return(message);} public void setMessage(String message) { this.message = message;} } |