• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with jsp:getProperty

 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Moorthi Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic