aspose file tools*
The moose likes JSP and the fly likes jsp:setProperty - setting all properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:setProperty - setting all properties" Watch "jsp:setProperty - setting all properties" New topic
Author

jsp:setProperty - setting all properties

Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

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.


Mike Firkser
Rutgers '84
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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)?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

I've changed the scope to "session" and I still get the same results.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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

Joined: Oct 21, 2003
Posts: 247

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

Joined: Jan 10, 2002
Posts: 61198
    
  66

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

Joined: Oct 21, 2003
Posts: 247

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

Joined: Jan 10, 2002
Posts: 61198
    
  66


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

Joined: Oct 21, 2003
Posts: 247

SUCCESS!!!
Thanks for the help. And since this is the Big Moose Saloon, next round is on me!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Sarsparillas for the house!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:setProperty - setting all properties