[jsp]: getProperty

M

Marcus Reiter

Hi, I got a Bean that represents a vector containing several Beans
containing
several attributes (mainly Strings).

How can I set / display those attributes?

I asked Google, but unforunately I can't find any fitting examples.
Maybe you could help.

( I am trying to solve this just by using jsp standard tags)

<jsp:useBean id="BeanName" class="packageName.BeanName"
scope="application"/>
<jsp:setProperty name="BeanName" property="vectorName" value="10"/>
<jsp:getProperty name="BeanName" property="vectorName"/>
<jsp:useBean id="IncludedBeanName" class="packageName.IncludedBeanName"
scope="application"/>
<jsp:getProperty name="vectorName" property="IncludedBeanName"/>
<jsp:getProperty name="IncludedBeanName" property="attributeName"/>

However, that doesn't work.
Here is the error it throws:
org.apache.jasper.JasperException: Can't find a method to write property
vectorNameof type 'java.util.Vector' in a bean of type
'packageName.BeanName'

Here is the java code of "BeanName.java":
private Vector vectorName= new Vector();
private dbConnection = new DbConnection();


public Vector getVectorName() {
return this.vectorName;
}

public void setVectorName(int attribute) {
this.vectorName= dbConnection .getBeanList(attribute);
}

Here is what happens in class "dbConnection.java":

Vector vectorName = new Vector();

while (rs.next()) {
beanName= new BeanName();
beanName.setAttribute1(rs.getInt(1)); // auslesen der
Attribute
beanName.setAttribute2(rs.getString(2) + " " +
rs.getString(3));
beanName.setsetAttribute3(rs.getInt(4));
beanName.setAttribute5(rs.getDate(5));
beanName.setAttribute6(rs.getString(6));
beanName.setAttribute7(rs.getString(7) + " " +
rs.getString(8));
vectorName .add(beanName);
}

Can you help?

Thanks,

Marcus
 
R

Ryan Stewart

[...]
<jsp:setProperty name="BeanName" property="vectorName" value="10"/>
[...]
org.apache.jasper.JasperException: Can't find a method to write property
vectorNameof type 'java.util.Vector' in a bean of type
'packageName.BeanName'
[...]
private Vector vectorName= new Vector();

[...]
"vectorName" is a Vector. The value "10" can't be converted to a Vector.
Consider a minor or major redesign. Name things appropriately, and don't
make a setter for a Vector property that takes an int (which is what's
screwing you up).
 
M

Marcus Reiter

10 should not get converted to a Vector.

The thing is the vector I want first of all needs to get builded -
and the method that builds that vector needs an integer value.

look, that's the method it should connect to:

public void setVectorName(int attribute) {
this.vectorName= dbConnection .getBeanList(attribute);
}

so it should take that int value, send it to a method in another class and
this method
should return a vector, that then gets saved in my bean.

I guess that might not be the best idea, yet it's the only idea I had so
far...

Marcus
 
R

Ryan Stewart

Marcus Reiter said:
10 should not get converted to a Vector.
Obviously, but that's what you've told it to do because the variable
"vectorName" is of type Vector.
The thing is the vector I want first of all needs to get builded -
and the method that builds that vector needs an integer value.

look, that's the method it should connect to:

public void setVectorName(int attribute) {
this.vectorName= dbConnection .getBeanList(attribute);
}

so it should take that int value, send it to a method in another class and
this method
should return a vector, that then gets saved in my bean.
The name of the method declares that it's a setter for property vectorName.
You've used it as something else.
I guess that might not be the best idea, yet it's the only idea I had so
far...
What about a different method like setType or setAttribute, where type or
attribute or whatever you want to call it is an int?
 
M

Marcus Reiter

Sounds good -

But how would I do that?

So far I only know the setProperty and getProperty tag...

Marcus
 
S

Sudsy

Marcus said:
Sounds good -

But how would I do that?

So far I only know the setProperty and getProperty tag...

Marcus,
What Ryan is trying to explain is that you have an impedance
mis-match here. Look at these two methods:

public Vector getVectorName() {
...

public void setVectorName(int attribute) {
...

So the accessor is returning a java.util.Vector but the mutator
is expecting an int. The argument type for the mutator is supposed
to be the same as the return type from the accessor, i.e.:

public Vector getVectorName() {
...

public void setVectorName( Vector name ) {
...

What Ryan is (I think) suggesting is that you add another attribute,
something like this:

public Vector getVectorName() {
...

public void setVectorNameIndex( int nameIndex ) {
...

public int getVectorNameIndex() {
...

The rest should be fairly obvious by now...
 
M

Marcus Reiter

What Ryan is (I think) suggesting is that you add another attribute,
something like this:

public Vector getVectorName() {
...

public void setVectorNameIndex( int nameIndex ) {
...

public int getVectorNameIndex() {
...

The rest should be fairly obvious by now...

Yeah, I got that part - but how do I use this method then?
I can't use the set or get Property then, can I?

I mean I would prefer to use some tag rather then using java code.
However, I tried this:

<%@page language="java" %>
<%! DBConnection dbconnection = new DBConnection (); %>
<jsp:useBean id="myBean" class="jsplogin.MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myVector" param="<%= dbconnection
..createMyVector(100033)%>"/>

Jbuilder throws the most weird exceptions:
'catch' without 'try'
'}' expected
'try' without 'catch' or 'finally'
"MyJSP.jsp": cannot find symbol; symbol : class DBConnection, location:
class org.apache.jsp.MyJSP_jsp
"MyJSP.jsp": cannot find symbol; symbol : class DBConnection, location:
class org.apache.jsp.MyJSP_jsp

So how does it work using a tag, and if that's not possible, how does it
work otherwise?

Thanks,

Marcus
 
R

Ryan Stewart

Marcus Reiter said:
Yeah, I got that part - but how do I use this method then?
I can't use the set or get Property then, can I?

I mean I would prefer to use some tag rather then using java code.
However, I tried this:

<%@page language="java" %>
<%! DBConnection dbconnection = new DBConnection (); %>
<jsp:useBean id="myBean" class="jsplogin.MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myVector" param="<%= dbconnection
.createMyVector(100033)%>"/>

Jbuilder throws the most weird exceptions:
'catch' without 'try'
'}' expected
'try' without 'catch' or 'finally'
"MyJSP.jsp": cannot find symbol; symbol : class DBConnection, location:
class org.apache.jsp.MyJSP_jsp
"MyJSP.jsp": cannot find symbol; symbol : class DBConnection, location:
class org.apache.jsp.MyJSP_jsp

So how does it work using a tag, and if that's not possible, how does it
work otherwise?
Sudsy knew what I was thinking. You're calling the wrong setter. Instead of
setting the Vector, set the index:
<jsp:setProperty nam="myBean" property="vectorNameIndex" value="10" />

Then in that index setter or elsewhere as you choose, write the code to
populate the Vector from the DB.

The exception you experienced is likely due to a syntax error in your JSP.
Before a JSP is run, it's translated to a servlet, which is straight Java
code. If you have a subtle error that the JSP parser doesn't catch, you'll
get an exception from the servlet like you see above. It could be a missing
"{" or "}" or maybe a String literal closed in the wrong place
inadvertently.
 
M

Marcus Reiter

Okay, finally this works:

<%@ page language="java" %>
<jsp:useBean id="myBean" class="myPackage.MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myIndex" value="10"/>
<jsp:getProperty name="myBean" property="myVector"/>

However, this vector is quite complicated -
it contains several Beans, each of them represents an entry of my database,
so it contains about 10 values I would like to print out.

How can that be done?

So far it just prints out the objects, which is just nonsense of course.
I thought I maybe could just acesss that dynamically created vector and use
it as if it was a Bean again
and try to acess the Beans inside it with another getProperty method -
something like that worked in Struts. ( Which I can't use in this case).

That's what I did to access my values, but it doesn't work:

<jsp:useBean id="myOtherBean" class="myPackage.MyOtherBean"
scope="application"/>
<jsp:getProperty name="myVector" property="myOtherBean"/>
<jsp:getProperty name="myOtherBean" property="myStringValues"/>

Any ideas?

Thanks a lot, as always!

Marcus
 
R

Ryan Stewart

Marcus Reiter said:
Okay, finally this works:

<%@ page language="java" %>
<jsp:useBean id="myBean" class="myPackage.MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myIndex" value="10"/>
<jsp:getProperty name="myBean" property="myVector"/>

However, this vector is quite complicated -
it contains several Beans, each of them represents an entry of my
database,
so it contains about 10 values I would like to print out.

How can that be done?

So far it just prints out the objects, which is just nonsense of course.
I thought I maybe could just acesss that dynamically created vector and
use
it as if it was a Bean again
and try to acess the Beans inside it with another getProperty method -
something like that worked in Struts. ( Which I can't use in this case).

That's what I did to access my values, but it doesn't work:

<jsp:useBean id="myOtherBean" class="myPackage.MyOtherBean"
scope="application"/>
<jsp:getProperty name="myVector" property="myOtherBean"/>
If you didn't just mix this line up then I think I see what you were trying
to do with it, but that isn't how it works. "name" is the name of the bean
to look in. It must mach the id of your useBean tag. "property" is the
property of the specified bean to retrieve.
<jsp:getProperty name="myOtherBean" property="myStringValues"/>

Any ideas?

Thanks a lot, as always!
Try JSTL. It would be something like
<c:forEach var="bean" items="${myBean.myVector}">
<c:eek:ut value="${bean.property1}" />
<c:eek:ut value="${bean.property2}" />
...
</c:forEach>

Otherwise I'm not sure there's a solution using just jsp tags. I'm no expert
on those, though. I have very little experience with them.
 
M

Marcus Reiter

I can't use JSTL for this task.

This is what I got now, and it works:

<%@ page language="java" %>
<%@ page import="java.util.Enumeration, java.util.Vector,
mypackage.DBConnection, mypackage.MyOtherBean" %>
<%@ page contentType="text/html" %>

<jsp:useBean id="myVector" class="java.util.Vector" scope="application"/>
<jsp:useBean id="myOtherBean" class="mypackage.MyOtherBean"
scope="application"/>
<jsp:useBean id="dbConnection" class="mypackage.DBConnection"
scope="application"/>

<%
myVector = dbConnection.getMyVector(10);
for (Enumeration el = myVector.elements();
el.hasMoreElements(); ) {
myOtherBean = (MyOtherBean) el.nextElement();
out.println(myOtherBean.getAttribute1());
out.println(myOtherBean.getAttribute2());
out.println(myOtherBean.getAttribute3());
}
%>

<!--
<jsp:useBean id="MyBean" class="mypackage.MyBean" scope="application"/>
<jsp:setProperty name="MyBean" property="myIndex" value="10"/>
<jsp:getProperty name="MyBean" property="myIndex" value="10"/>
<jsp:getProperty name="myBean" property="myVector"/>
-->

But I am still looking for a way of how to use less script code.
Is there a way to retrieve a property from a Bean using the "getProperty"
Tag and then to use it like
a regular variable?

Something like:
<jsp:getProperty name="myBean" property="myVector"/>
<!-- myVector can now be used like a variable -->
for (Enumeration el = myVector.elements();
el.hasMoreElements(); ) {
myOtherBean = (MyOtherBean) el.nextElement();
out.println(myOtherBean.getAttribute1());
out.println(myOtherBean.getAttribute2());
out.println(myOtherBean.getAttribute3());
}

But "getProperty" just seems to print out the vector - I doesn't seem to be
able to
use it like a regular variable in my java code.
?

Any ideas?

Thanks a lot,

Marcus
 
R

Ryan Stewart

Marcus Reiter said:
I can't use JSTL for this task.
That's odd. Why not?
This is what I got now, and it works:
[...]
Right, I meant to say you could use scriptlets if you wanted, but you seem
to have picked up on that anyway.
But I am still looking for a way of how to use less script code.
Is there a way to retrieve a property from a Bean using the "getProperty"
Tag and then to use it like
a regular variable?
[...]
I'm *almost* sure that's not possible. But like I said, I'm no expert on the
jsp tags. I mainly use JSTL and Struts. The jsp tags are for simple beans,
not the stuff you're trying to do. You need something more flexible. You
might take a look at you object model and see if you can come up with a
better way of doing it, but with the complexity of what you've posted here,
you may have a hard time squeezing it into simple getters and setters.
 
M

Marcus Reiter

Okay, fine.
The reason is - this is part of a task for uni,
and in that exercise we are only allowed to use plain jsp
and only to use skriptlet code if really necessary.

I can't find a way, but the prof. said we should put the values coming from
the
database into a bean and then to retrieve them from there.
But the problem is that the database is full of several entries and
so I had to put each entry into a bean and each bean into a vector.
Now I don't see a way of how to print out each value by using the
getProperty / setProperty Tag.

Even if the Vector would contain plain Strings instead of Beans -
then it would print out the values without html formating -
of course I could even include that into my vector -
but then html code and data is mixed and that's even worse imho!

What do you think?

Marcus
 
R

Ryan Stewart

Marcus Reiter said:
Okay, fine.
The reason is - this is part of a task for uni,
and in that exercise we are only allowed to use plain jsp
and only to use skriptlet code if really necessary.

I can't find a way, but the prof. said we should put the values coming
from
the
database into a bean and then to retrieve them from there.
But the problem is that the database is full of several entries and
so I had to put each entry into a bean and each bean into a vector.
Now I don't see a way of how to print out each value by using the
getProperty / setProperty Tag.

Even if the Vector would contain plain Strings instead of Beans -
then it would print out the values without html formating -
of course I could even include that into my vector -
but then html code and data is mixed and that's even worse imho!

What do you think?
Are you sure you're not supposed to be getting just one entry from the
database at a time and creating a single bean based on it? On what basis are
you retrieving values from the database? What's your key, so to speak? I
know before you were setting an int, which caused a Vector to be populated.
Why does a key have multiple values? *If* you've been doing something wrong,
and each key is only supposed to get one value, it's not too hard. I'm sure
you could figure that out. But something here isn't quite adding up. If
you're not supposed to use scriptlets or other libraries, and each key has
to correspond to multiple bean values... I'm just not seeing a clean way of
doing it. Since nobody else has jumped into this thread, I assume that they
aren't having any bright ideas either.

I just had a thought, though. Have you covered expression language at all?
I'm not aware of the full use of EL in JSP 2.0 since I'm tied to the older
spec at the moment, but it is quite powerful.
 
A

Andrew Thompson

..If
you're not supposed to use scriptlets or other libraries, and each key has
to correspond to multiple bean values... I'm just not seeing a clean way of
doing it. Since nobody else has jumped into this thread, I assume that they
aren't having any bright ideas either.

Dunno if it is bright or not, but I'd take it back to the prof.,
explain your current strategy and reasons for it, and ask her/him where
you are going wrong.

It could turn out to be a simple misunderstanding of the requirement
or constraints.
 
S

Sudsy

Ryan said:
Are you sure you're not supposed to be getting just one entry from the
database at a time and creating a single bean based on it? On what basis are
you retrieving values from the database? What's your key, so to speak? I
know before you were setting an int, which caused a Vector to be populated.
Why does a key have multiple values? *If* you've been doing something wrong,
and each key is only supposed to get one value, it's not too hard. I'm sure
you could figure that out. But something here isn't quite adding up. If
you're not supposed to use scriptlets or other libraries, and each key has
to correspond to multiple bean values... I'm just not seeing a clean way of
doing it. Since nobody else has jumped into this thread, I assume that they
aren't having any bright ideas either.
<snip>

Ryan,
You and I did everything but write the code which would have made the
original post work. OP then went off on various tangents. Now we find
out that it's a uni assignment. I didn't want to get one of my spoons
dirty (or spend the time) providing the complete solution based on the
original post. I /assumed/ (my mistake) that the OP had a least a
modicum of Java experience, else why would they be jumping into JSPs?
The mods should have taken just a few moments: change the name of the
mutator in the bean code and the property name in the jsp:setProperty
tag. So why has this thread continued? Have the requirements changed?
Why isn't OP asking his prof, someone who's actually getting PAID to
provide assistance?
 
M

Marcus Reiter

1. I would love to ask my prof but it's currently winter break so I can't
ask.
2. It has to be finished after this break.

3. change the name of the
mutator in the bean code and the property name in the jsp:setProperty
tag.

I have done that. But you don't seem to have read what I really wrote..

Marcus
 
A

Andrew Thompson

1. I would love to ask my prof but it's currently winter break so I can't
ask.
2. It has to be finished after this break.

So you have left this too late. That is bad for you.

Was there a compelling reason, such as a family/life crisis, flood
or earthquake[1]? Or was it more tardyness and bad planning[2]?

[1] Which may gain you a reprieve in the form of an extension.
[2] Which will not cut much slack, there *or* here.
 
R

Ryan Stewart

Marcus Reiter said:
I can't find a way, but the prof. said we should put the values coming
from
the
database into a bean and then to retrieve them from there.
But the problem is that the database is full of several entries and
so I had to put each entry into a bean and each bean into a vector.
Now I don't see a way of how to print out each value by using the
getProperty / setProperty Tag.

Even if the Vector would contain plain Strings instead of Beans -
then it would print out the values without html formating -
of course I could even include that into my vector -
but then html code and data is mixed and that's even worse imho!
I had a sneaky idea:

public class InfoBean {
public InfoBean() {
// Database calls to populate this bean with first available data
}
public String getProperty1 { return something; }
public String getProperty2 { return somethingElse; }
public String getProperty3 {
String returnValue = somethingElsesBrother;
// Database calls to populate this same bean with the next data
return returnValue;
}
}

Then you can just repeatedly use getProperty on the properties, and it will
automatically reset after the last one. The only problem then is knowing how
many times to cycle through.

As an aside, I would never do something like this in general practice. This
is a case of using questionable code to meet questionable requirements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top