You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Tathagat <ma...@tathagat.com> on 2007/06/20 17:28:38 UTC

File attachment - view state couldn't be restored, reload?

Hi all.
In my JSF application I have a link (t:commandLink) on click of which I want
to send an attachment to the user. The usual way in struts was

snippet:
response.setHeader ("Content-Disposition","attachment;filename=\"mae.csv\
"");
response.setContentType("text/csv");
response.getOutputStream().write(file.getBytes()); // file is some object
which return bytes
response.getOutputStream().flush();
response.getOutputStream().close();

When I do this in JSF, I also do in the end:
FacesContext.getCurrentInstance().responseComplete();

Works fine - But after I have the file, I try to do something else (Another
action), I get the error:
*View state couldn't be restored, reload?*
I guess it is because JSF loses the view (for some reason I don't know).

What is the right way to send an attachment to user with *PURE JSF*solution.

I read in some places about using shale, servlets, etc.. But I want a pure
JSF solution. Help, anyone?

Thanks in advance.

Cheers.

Re: File attachment - view state couldn't be restored, reload?

Posted by Matthias Wessendorf <ma...@apache.org>.
can you file a JIRA issue ?

On 6/21/07, Tathagat <ma...@tathagat.com> wrote:
> FIXED.
> The problem was in the t:commandLink. if I use h:commandButton it works
> fine.
>
> Why, anyone has any idea?
>
> Cheers.
>
>
>
> On 6/21/07, Tathagat <ma...@tathagat.com> wrote:
> >
> > Thanks Bruno.
> >
> > Now I do this.
> >
> _____________________________________________________________
> >    HttpServletResponse response =
> (HttpServletResponse)context.getExternalContext().getResponse();
> >    response.setHeader
> ("Content-Disposition","attachment;filename=\"mae.csv\"");
> >    response.setContentType("text/csv");
> >
> >    response.getOutputStream().write(sbExcel.toString().getBytes());
> >    response.getOutputStream().flush();
> >    response.getOutputStream ().close();
> >
> >    StateManager stateManager =
> (StateManager)context.getApplication().getStateManager();
> >    stateManager.saveSerializedView(context);
> >
> >    FacesContext.getCurrentInstance ().responseComplete();
> >
> _____________________________________________________________
> >
> > But I get an error - see attached image (image.jpg).
> >
> > Thanks in advance.
> >
> >
> >
> > On 6/21/07, Mikael Andersson <mail.micke@gmail.com > wrote:
> > > Thanks Bruno,
> > > will try that.
> > >
> > >
> > > PS.
> > > Hope Cambridge is treating you well :)
> > >
> > >
> > >
> > >
> > > On 21/06/07, Bruno Aranda < brunoaranda@gmail.com > wrote:
> > > > Hi,
> > > >
> > > > Before calling responseComplete(), that makes the lifecycle end, you
> > > > may need to save the state of the view by using the StateManager,
> > > >
> > > > StateManager stateManager = (StateManager)
> > > > faces.getApplication().getStateManager();
> > > > stateManager.saveSerializedView(faces);
> > > >
> > > > Calling it you save the component state information, used to restore
> the view...
> > > >
> > > > Hope it helps,
> > > >
> > > > Bruno
> > > >
> > > > PS. Mikael :-)
> > > >
> > > > On 21/06/07, Mikael Andersson <mail.micke@gmail.com > wrote:
> > > > > Hi
> > > > >
> > > > > I'm experiencing the same problem, and would also be interested in
> knowing
> > > > > if there are any solutions which don't involve bringing in another
> > > > > framework.
> > > > >
> > > > > If there isn't a solution, it would be great if someone could
> explain to me
> > > > > the reason why this happens.
> > > > > I think/guess it has something to do with the view state being build
> after
> > > > > render response, and by calling responseComplete that phase isn't
> invoked.
> > > > > Is that accurate?
> > > > >
> > > > > Cheers,
> > > > >  Mike
> > > > >
> > > > >
> > > > >  On 20/06/07, Tathagat < mail@tathagat.com> wrote:
> > > > > >
> > > > > > Hi all.
> > > > > > In my JSF application I have a link (t:commandLink) on click of
> which I
> > > > > want to send an attachment to the user. The usual way in struts was
> > > > > >
> > > > > > snippet:
> > > > > > response.setHeader
> > > > >
> ("Content-Disposition","attachment;filename=\"mae.csv\ "");
> > > > > > response.setContentType("text/csv");
> > > > > > response.getOutputStream().write(file.getBytes()); // file is some
> object
> > > > > which return bytes
> > > > > > response.getOutputStream ().flush();
> > > > > > response.getOutputStream().close();
> > > > > >
> > > > > > When I do this in JSF, I also do in the end:
> > > > > > FacesContext.getCurrentInstance().responseComplete();
> > > > > >
> > > > > > Works fine - But after I have the file, I try to do something else
> > > > > (Another action), I get the error:View state couldn't be restored,
> reload?
> > > > > >
> > > > > > I guess it is because JSF loses the view (for some reason I don't
> know).
> > > > > >
> > > > > > What is the right way to send an attachment to user with PURE JSF
> > > > > solution.
> > > > > >
> > > > > > I read in some places about using shale, servlets, etc.. But I
> want a pure
> > > > > JSF solution. Help, anyone?
> > > > > >
> > > > > > Thanks in advance.
> > > > > >
> > > > > > Cheers.
> > > > >
> > > > >
> > > >
> > >
> > >
> >
> >
> >
>
>


-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
mail: matzew-at-apache-dot-org

Re: File attachment - view state couldn't be restored, reload?

Posted by Tathagat <ma...@tathagat.com>.
FIXED.
The problem was in the *t:commandLink*.* *if I use *h:commandButton *it
works fine.

Why, anyone has any idea?

Cheers.


On 6/21/07, Tathagat <ma...@tathagat.com> wrote:
>
> Thanks Bruno.
>
> Now I do this.
> _____________________________________________________________
>    HttpServletResponse response =
> (HttpServletResponse)context.getExternalContext().getResponse();
>    response.setHeader ("Content-Disposition","attachment;filename=\"
> mae.csv\"");
>    response.setContentType("text/csv");
>
>    response.getOutputStream().write(sbExcel.toString().getBytes());
>    response.getOutputStream().flush();
>    response.getOutputStream().close();
>
>    *StateManager stateManager =
> (StateManager)context.getApplication().getStateManager();
>    stateManager.saveSerializedView(context);*
>
>    FacesContext.getCurrentInstance().responseComplete();
> _____________________________________________________________
>
> But I get an error - see attached image (image.jpg).
>
> Thanks in advance.
>
>
>  On 6/21/07, Mikael Andersson <ma...@gmail.com> wrote:
> >
> > Thanks Bruno,
> > will try that.
> >
> >
> > PS.
> > Hope Cambridge is treating you well :)
> >
> >
> > On 21/06/07, Bruno Aranda < brunoaranda@gmail.com > wrote:
> > >
> > > Hi,
> > >
> > > Before calling responseComplete(), that makes the lifecycle end, you
> > > may need to save the state of the view by using the StateManager,
> > >
> > > StateManager stateManager = (StateManager)
> > > faces.getApplication().getStateManager();
> > > stateManager.saveSerializedView(faces);
> > >
> > > Calling it you save the component state information, used to restore
> > > the view...
> > >
> > > Hope it helps,
> > >
> > > Bruno
> > >
> > > PS. Mikael :-)
> > >
> > > On 21/06/07, Mikael Andersson <mail.micke@gmail.com > wrote:
> > > > Hi
> > > >
> > > > I'm experiencing the same problem, and would also be interested in
> > > knowing
> > > > if there are any solutions which don't involve bringing in another
> > > > framework.
> > > >
> > > > If there isn't a solution, it would be great if someone could
> > > explain to me
> > > > the reason why this happens.
> > > > I think/guess it has something to do with the view state being build
> > > after
> > > > render response, and by calling responseComplete that phase isn't
> > > invoked.
> > > > Is that accurate?
> > > >
> > > > Cheers,
> > > >  Mike
> > > >
> > > >
> > > >  On 20/06/07, Tathagat < mail@tathagat.com> wrote:
> > > > >
> > > > > Hi all.
> > > > > In my JSF application I have a link (t:commandLink) on click of
> > > which I
> > > > want to send an attachment to the user. The usual way in struts was
> > > > >
> > > > > snippet:
> > > > > response.setHeader
> > > > ("Content-Disposition","attachment;filename=\"mae.csv\ "");
> > > > > response.setContentType("text/csv");
> > > > > response.getOutputStream().write(file.getBytes()); // file is some
> > > object
> > > > which return bytes
> > > > > response.getOutputStream ().flush();
> > > > > response.getOutputStream().close();
> > > > >
> > > > > When I do this in JSF, I also do in the end:
> > > > > FacesContext.getCurrentInstance().responseComplete();
> > > > >
> > > > > Works fine - But after I have the file, I try to do something else
> > >
> > > > (Another action), I get the error:View state couldn't be restored,
> > > reload?
> > > > >
> > > > > I guess it is because JSF loses the view (for some reason I don't
> > > know).
> > > > >
> > > > > What is the right way to send an attachment to user with PURE JSF
> > > > solution.
> > > > >
> > > > > I read in some places about using shale, servlets, etc.. But I
> > > want a pure
> > > > JSF solution. Help, anyone?
> > > > >
> > > > > Thanks in advance.
> > > > >
> > > > > Cheers.
> > > >
> > > >
> > >
> >
> >
>
>

Re: File attachment - view state couldn't be restored, reload?

Posted by Tathagat <ma...@tathagat.com>.
Thanks Bruno.

Now I do this.
_____________________________________________________________
   HttpServletResponse response =
(HttpServletResponse)context.getExternalContext().getResponse();
   response.setHeader ("Content-Disposition","attachment;filename=\"mae.csv\
"");
   response.setContentType("text/csv");

   response.getOutputStream().write(sbExcel.toString().getBytes());
   response.getOutputStream().flush();
   response.getOutputStream().close();

   *StateManager stateManager =
(StateManager)context.getApplication().getStateManager();
   stateManager.saveSerializedView(context);*

   FacesContext.getCurrentInstance().responseComplete();
_____________________________________________________________

But I get an error - see attached image (image.jpg).

Thanks in advance.


On 6/21/07, Mikael Andersson <ma...@gmail.com> wrote:
>
> Thanks Bruno,
> will try that.
>
>
> PS.
> Hope Cambridge is treating you well :)
>
>
> On 21/06/07, Bruno Aranda < brunoaranda@gmail.com> wrote:
> >
> > Hi,
> >
> > Before calling responseComplete(), that makes the lifecycle end, you
> > may need to save the state of the view by using the StateManager,
> >
> > StateManager stateManager = (StateManager)
> > faces.getApplication().getStateManager();
> > stateManager.saveSerializedView(faces);
> >
> > Calling it you save the component state information, used to restore the
> > view...
> >
> > Hope it helps,
> >
> > Bruno
> >
> > PS. Mikael :-)
> >
> > On 21/06/07, Mikael Andersson <ma...@gmail.com> wrote:
> > > Hi
> > >
> > > I'm experiencing the same problem, and would also be interested in
> > knowing
> > > if there are any solutions which don't involve bringing in another
> > > framework.
> > >
> > > If there isn't a solution, it would be great if someone could explain
> > to me
> > > the reason why this happens.
> > > I think/guess it has something to do with the view state being build
> > after
> > > render response, and by calling responseComplete that phase isn't
> > invoked.
> > > Is that accurate?
> > >
> > > Cheers,
> > >  Mike
> > >
> > >
> > >  On 20/06/07, Tathagat <ma...@tathagat.com> wrote:
> > > >
> > > > Hi all.
> > > > In my JSF application I have a link (t:commandLink) on click of
> > which I
> > > want to send an attachment to the user. The usual way in struts was
> > > >
> > > > snippet:
> > > > response.setHeader
> > > ("Content-Disposition","attachment;filename=\"mae.csv\ "");
> > > > response.setContentType("text/csv");
> > > > response.getOutputStream().write(file.getBytes()); // file is some
> > object
> > > which return bytes
> > > > response.getOutputStream ().flush();
> > > > response.getOutputStream().close();
> > > >
> > > > When I do this in JSF, I also do in the end:
> > > > FacesContext.getCurrentInstance().responseComplete();
> > > >
> > > > Works fine - But after I have the file, I try to do something else
> > > (Another action), I get the error:View state couldn't be restored,
> > reload?
> > > >
> > > > I guess it is because JSF loses the view (for some reason I don't
> > know).
> > > >
> > > > What is the right way to send an attachment to user with PURE JSF
> > > solution.
> > > >
> > > > I read in some places about using shale, servlets, etc.. But I want
> > a pure
> > > JSF solution. Help, anyone?
> > > >
> > > > Thanks in advance.
> > > >
> > > > Cheers.
> > >
> > >
> >
>
>

Re: File attachment - view state couldn't be restored, reload?

Posted by Mikael Andersson <ma...@gmail.com>.
Thanks Bruno,
will try that.


PS.
Hope Cambridge is treating you well :)


On 21/06/07, Bruno Aranda <br...@gmail.com> wrote:
>
> Hi,
>
> Before calling responseComplete(), that makes the lifecycle end, you
> may need to save the state of the view by using the StateManager,
>
> StateManager stateManager = (StateManager)
> faces.getApplication().getStateManager();
> stateManager.saveSerializedView(faces);
>
> Calling it you save the component state information, used to restore the
> view...
>
> Hope it helps,
>
> Bruno
>
> PS. Mikael :-)
>
> On 21/06/07, Mikael Andersson <ma...@gmail.com> wrote:
> > Hi
> >
> > I'm experiencing the same problem, and would also be interested in
> knowing
> > if there are any solutions which don't involve bringing in another
> > framework.
> >
> > If there isn't a solution, it would be great if someone could explain to
> me
> > the reason why this happens.
> > I think/guess it has something to do with the view state being build
> after
> > render response, and by calling responseComplete that phase isn't
> invoked.
> > Is that accurate?
> >
> > Cheers,
> >  Mike
> >
> >
> >  On 20/06/07, Tathagat <ma...@tathagat.com> wrote:
> > >
> > > Hi all.
> > > In my JSF application I have a link (t:commandLink) on click of which
> I
> > want to send an attachment to the user. The usual way in struts was
> > >
> > > snippet:
> > > response.setHeader
> > ("Content-Disposition","attachment;filename=\"mae.csv\"");
> > > response.setContentType("text/csv");
> > > response.getOutputStream().write(file.getBytes()); // file is some
> object
> > which return bytes
> > > response.getOutputStream().flush();
> > > response.getOutputStream().close();
> > >
> > > When I do this in JSF, I also do in the end:
> > > FacesContext.getCurrentInstance().responseComplete();
> > >
> > > Works fine - But after I have the file, I try to do something else
> > (Another action), I get the error:View state couldn't be restored,
> reload?
> > >
> > > I guess it is because JSF loses the view (for some reason I don't
> know).
> > >
> > > What is the right way to send an attachment to user with PURE JSF
> > solution.
> > >
> > > I read in some places about using shale, servlets, etc.. But I want a
> pure
> > JSF solution. Help, anyone?
> > >
> > > Thanks in advance.
> > >
> > > Cheers.
> >
> >
>

Re: File attachment - view state couldn't be restored, reload?

Posted by Bruno Aranda <br...@gmail.com>.
Hi,

Before calling responseComplete(), that makes the lifecycle end, you
may need to save the state of the view by using the StateManager,

StateManager stateManager = (StateManager)
faces.getApplication().getStateManager();
stateManager.saveSerializedView(faces);

Calling it you save the component state information, used to restore the view...

Hope it helps,

Bruno

PS. Mikael :-)

On 21/06/07, Mikael Andersson <ma...@gmail.com> wrote:
> Hi
>
> I'm experiencing the same problem, and would also be interested in knowing
> if there are any solutions which don't involve bringing in another
> framework.
>
> If there isn't a solution, it would be great if someone could explain to me
> the reason why this happens.
> I think/guess it has something to do with the view state being build after
> render response, and by calling responseComplete that phase isn't invoked.
> Is that accurate?
>
> Cheers,
>  Mike
>
>
>  On 20/06/07, Tathagat <ma...@tathagat.com> wrote:
> >
> > Hi all.
> > In my JSF application I have a link (t:commandLink) on click of which I
> want to send an attachment to the user. The usual way in struts was
> >
> > snippet:
> > response.setHeader
> ("Content-Disposition","attachment;filename=\"mae.csv\"");
> > response.setContentType("text/csv");
> > response.getOutputStream().write(file.getBytes()); // file is some object
> which return bytes
> > response.getOutputStream().flush();
> > response.getOutputStream().close();
> >
> > When I do this in JSF, I also do in the end:
> > FacesContext.getCurrentInstance().responseComplete();
> >
> > Works fine - But after I have the file, I try to do something else
> (Another action), I get the error:View state couldn't be restored, reload?
> >
> > I guess it is because JSF loses the view (for some reason I don't know).
> >
> > What is the right way to send an attachment to user with PURE JSF
> solution.
> >
> > I read in some places about using shale, servlets, etc.. But I want a pure
> JSF solution. Help, anyone?
> >
> > Thanks in advance.
> >
> > Cheers.
>
>

Re: File attachment - view state couldn't be restored, reload?

Posted by Mikael Andersson <ma...@gmail.com>.
Hi

I'm experiencing the same problem, and would also be interested in knowing
if there are any solutions which don't involve bringing in another
framework.

If there isn't a solution, it would be great if someone could explain to me
the reason why this happens.
I think/guess it has something to do with the view state being build after
render response, and by calling responseComplete that phase isn't invoked.
Is that accurate?

Cheers,
 Mike

On 20/06/07, Tathagat <ma...@tathagat.com> wrote:
>
> Hi all.
> In my JSF application I have a link (t:commandLink) on click of which I
> want to send an attachment to the user. The usual way in struts was
>
> snippet:
> response.setHeader ("Content-Disposition","attachment;filename=\"mae.csv\
> "");
> response.setContentType("text/csv");
> response.getOutputStream().write(file.getBytes()); // file is some object
> which return bytes
> response.getOutputStream().flush();
> response.getOutputStream().close();
>
> When I do this in JSF, I also do in the end:
> FacesContext.getCurrentInstance().responseComplete();
>
> Works fine - But after I have the file, I try to do something else
> (Another action), I get the error:
> *View state couldn't be restored, reload?*
> I guess it is because JSF loses the view (for some reason I don't know).
>
> What is the right way to send an attachment to user with *PURE JSF*solution.
>
> I read in some places about using shale, servlets, etc.. But I want a pure
> JSF solution. Help, anyone?
>
> Thanks in advance.
>
> Cheers.
>