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.
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
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?
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 ]