File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problem with jsp:getProperty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with jsp:getProperty" Watch "Problem with jsp:getProperty" New topic
Author

Problem with jsp:getProperty

Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
In my jsp I am using <jsp:getProperty> to get the value of a bean
Here is the code sample
<jsp:useBean id="test" scope="session" class="TestBean"/>
<tr>
<td width="60%" align="right"><b><font color="#000080">First Name #</font></b>
</td>
<td width="40%"><input type="text" name="fName" size="20"
value="<jsp:getProperty name="test" property="firstName" />"></td>
</tr>

I try to use the <jsp:getProperty> tag to get the value
but it keeps initiating a "blank" bean.
As a temporary workaround, I am using <% out.println(TestBean.getFirstName()) %>
to achieve the same effect and it works.
Ideally, I would like to use <jsp:getProperty name="beanName" property="beanID">
And some reason its not working
The firstName is declared as a variable in a Bean and and it also
has a corresponding get method
Guru's Please help me
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Your examples are a tad confusing.
In your first example (the one that doesn't work), you refer to the bean with an id of test.
In your second (the scriptlet example), you use TestBean which is the class name.
What's up with that?
Is getFirstName() a class (static) method?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
No Its not a static method. Sorry for creating any confusion.
I am creating instance of TestBean class
TestBean testBean= new TestBean();
and trying to get the value like this <% out.println(testBean.getFirstName()) %>
Thanks
Maria
Mehdi Chaouachi
Ranch Hand

Joined: Jul 02, 2003
Posts: 87
try instead using the <jsp:useBean id-"test".../> first and then try to acces it via the scriplet code like thgis <%= test.getFirstName()%> and see what result it gives you.


Mehdi Chaouachi<p>Sun Certified Java Programer (1.4)<br />Sun Certified Web Component Developer (1.4).<br />Sun Certified Mobile Application Developer.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
try instead using the <jsp:useBean id-"test".../> first and then try to acces it via the scriplet code like thgis <%= test.getFirstName()%> and see what result it gives you.
That won't work.
The "usebean" tag creates an object and stores it in the named context. It does not create a scriptlet variable. Likewise, just creating a scriptlet variable does not create or affect session beans.
To access a "usebean" object from a scriptlet you need to do something more like:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Moorthi Ramasamy
Greenhorn

Joined: Jan 02, 2004
Posts: 15
This is what I found. beanInstanceName "mine" is accessable in scriptlets & expressions.

JSP:
<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="myName" size="100"
value="<jsp:getProperty name="mine" property="count" />">
JAVABEAN :
package examples;
import java.io.*;
public class MyBean implements Serializable
{private String count = "58";
// Constuctors
public MyBean() { }
public MyBean(String count) {
this.count = count;
}
public String getCount(){
System.out.println("Count "+count
);
return this.count;
}
public void setCount(String count){
this.count = count;
}
}
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
Thank you so much for your help.
I use <jsp:getProperty> in all of my JSP and it just works fine.
I am having this problem only when
jsp property name(count) is different from text box name(myName)
for example

<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="myName" size="100"
value="<jsp:getProperty name="mine" property="count" />

Do you think is this a problem?

In all other JSP jsp propery name and text box name are the same ex
<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="count" size="100"
value="<jsp:getProperty name="mine" property="count" />
According to me it should not make any difference.
But some reason it is not working
Please help
Thanks in advance
Maria
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with jsp:getProperty
 
Similar Threads
Tomcat 5.0.19 and JSTL
Dynamic JSP
Struts Validation Problem
Getting the name of the requesting page
Suggest the Directory location in JSP program