File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:setProperty - setting all properties

 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've been working with Java just over a year, and now I'm just starting using taglibs while I'm trying to learn Stuts. I'm working on a WebLogic 8.1 server.
I've created a bean called "CaseInfoJavaBean" and it implements Serializable. It contains 6 member variable, all are Strings. My Action class creates a CaseInfoJavaBean object and sets it as a session attribute. I then transfer to my .jsp page and create the bean with this code:
<jsp:useBean id="caseInfoJavaBean" scope="page" class="cdrs.struts.CaseInfoJavaBean" />
I then try to populate the bean with this code:
<jsp:setProperty name="caseInfoJavaBean" property="*" />
Now the way I understand the setProperty tag, that should find a session object and assign it to the bean. When I try to print out the values, all but the first value are nulls. This is corrected if I insert this code:
<% caseInfoJavaBean = (cdrs.struts.CaseInfoJavaBean)session.getAttribute("CaseInfoJavaBean"); %>
I have no problem using the getProperty and setProperty on individual fields.
Thanks for any help you can give.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say:

... creates a CaseInfoJavaBean object and sets it as a session attribute.

and yet

<jsp:useBean id="caseInfoJavaBean" scope="page" class="cdrs.struts.CaseInfoJavaBean" />

Why are you looking for the bean in page scope when you have set it on the session (and why not the request unless this bean needs to "live" across multiple requests)?
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've changed the scope to "session" and I still get the same results.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it that you are expecting

to do for you?
Nowhere do you mention what request parameters are being submitted to the request that's executing your action and ending up on the JSP page.
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Action class, I create a CaseInfoJavaBean object and set it as an attribute, now on this page, I want to assign it to the bean using the <jsp:setProperty > tag. I've tried setting different parameters (the name of the class and stuff like that, with no success. The only way I can populate the entire bean with one line of code is
<% caseInfoJavaBean = (cdrs.struts.CaseInfoJavaBean)session.getAttribute("CaseInfoJavaBean"); %>
What do I need to add to the setProperty Tag so it sets the values to the entire bean? Can it be done with one line of code?
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do I need to add to the setProperty Tag so it sets the values to the entire bean?

I'm still not sure what you are trying to accomplish. What values are you trying to set? Where do you expect the values to come from?
The "*" mechanism of the jsp:setProperty tag will match up request parameters that were submitted to the current request with properties on the bean that have the corresponding setters defined. Is this what you are trying to do?
If so, then the "*" mechanism should work for you. If not, then you need to explain what you are trying to set the bean properties to and from what.
Follow-on question: since you are creating the bean in the action, why not set its properties there?
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm approaching this wrong.
The class is instantiated in the Action, and now I want it available on the JSP page. That is why I set it as a session attribute.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The class is instantiated in the Action, and now I want it available on the JSP page. That is why I set it as a session attribute.

If the bean is only needed for the JSP page, it's best to tack it onto the request rather than the session. That way it'll be cleaned up automatically when the request goes out of scope. You should reserve putting beans on the session for when the bean needs to stick around across the lifetime of multiple pages.
Other than that, what you are doing is fine and a common practice. The bean is instantiated in the controller (servlet/action), added as an attribute to the appropriate context (in this case, request), and then made available to the forwarded JSP page using the jsp:useBean tag. So far, so good. (You are getting to the JSP page from the action via a forward, no?)
Now remains the question of the bean properties. If you set them in the action/servlet to whatever is appropriate, they'll still be there when it gets to the JSP page. No further setting will be necessary unless you have a reason to defer setting the properties until the bean gets to the JSP. So, where are the values that you want to set the bean properties to coming from?
[ February 04, 2004: Message edited by: Bear Bibeault ]
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SUCCESS!!!
Thanks for the help. And since this is the Big Moose Saloon, next round is on me!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarsparillas for the house!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic