Peter Bishop wrote:
>
> -----Original Message-----
> From: A. C. [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, February 01, 2001 1:59 PM
> To: [EMAIL PROTECTED]
> Subject: Re: refresh of Session scoped beans
>
> > A recent thread out here has me playing with the "scope" attribute of the
> > <jsp:useBean> tag. Unfortunately, I have hit a wall. When using a bean of
> > scope="session", which is populated from an HTML form using a
> > <jsp:setProperty name="foo" property="*"/>. The initial population of the
> > bean is OK, however when I hit "back" on the browser to make corrections
> to
> > the form data, subsequent updates of the beans information are not
> > successful. This is alleviated by using a bean of scope="request", however
> > that is not what I need. Anyone encounter this problem and have a
> > solution???
>
> I solved the problem "resetting" the bean.
> Instead of the useBean tag, i use:
> myBeanClass bean = new myBeanClass();
> session.setAttribute("id bean istance", bean)
> So each time u acces to this page, the bean is resetted to the deafult
> value.
> After this, u'll have to put the setProperty * tag to initialize the bean
> with the new values submitted.
> P.S.: Remember that using the setProperty * feature, the corresponding set
> methods are called only if the corresponding form field is not empty. I
> mean, if u fill & submit the form, then u come back, delete a field and then
> submit again, the field u deleted won't be set to a null value in the bean
> but it will keep the old value. It's better to use manually the set property
> methods: bean.setProperty1("value") for each fields of the form.
> ====================================================================
> Thanks for the reply.
>
> I agree that this will solve the problem, but at the same time doesn't that
> defeat alot of the "convenience" associated with the JSP/Beans combo? For a
> large form, that is alot of bean.setProperty... calls.
>
> I wonder if there is a better way?
There is, and I believe someone else already suggested it: move the
<jsp:setProperty> element out of the body of your <jsp:useBean> action.
If you have it within the body, it's only executed when the bean is
created. Since you create a session bean, the bean exists on the second
request. Hence, <jsp:setProperty> is not executed the second time.
If you move it outside the body, it's executed even when the bean exists:
<jsp:useBean id="userInfo" class="com.foo.UserInfoBean" scope="session" />
<jsp:setProperty name="userInfo" property="*" />
> Thanks for the heads up on the blank form fields are not set to null thing.
> I'm sure that would have bothered me in the future.
One way to solve this is to add a setReset(String foo) method to your bean
that resets all bean property values to their default values (e.g. null).
Then add a <jsp:setProperty> action to call it before you set the properties
based on the new request parameters:
<jsp:useBean id="userInfo" class="com.foo.UserInfoBean" scope="session" />
<jsp:setProperty name="userInfo" property="reset" value="anything" />
<jsp:setProperty name="userInfo" property="*" />
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets