You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Carsten Kaiser <ca...@valtech.de> on 2007/03/27 13:18:13 UTC

How to add inline javascript in application phase?

Hello,



I'm facing the following problem:

I have an action, in which I want to trigger a popup window, in case the
triggered background job failed. According to the MyFaces Wiki

this can be achieved by



        FacesContext context = getFacesContext();

        ViewHandler viewHandler = getApplication().getViewHandler();

        String actionUrl = viewHandler.getActionURL(context, a_viewId);

        StringBuffer javaScriptText = new StringBuffer("window.open('");

        javaScriptText.append(actionUrl);

        javaScriptText.append("', 'popupWindow', 'dependent=yes,
menubar=no, toolbar=no');");

        AddResource addResource =
AddResourceFactory.getInstance(context);

        addResource.addInlineScriptAtPosition(context,
AddResource.HEADER_BEGIN, javaScriptText.toString());



Unfortunately the ResponseWriter is not yet set on the FacesContext
during this phase, so a NPE occurs when adding the inline script code

like in the last code line shown above. As far as I figured out the
response writer is setup/initialized in the render response phase. So
how could

this code ever worked? Trying to create a response writer manually just
messed up the whole view... (Later works for the AJAX stuff, but I

guess, that is because the normal response isn't rendered in this case!)



Any hints welcome!



Regards,
CK

-- 
Carsten Kaiser
Principal Consultant
mailto:carsten.kaiser@valtech.de
Mobile: +49 (0)170 5270206

Valtech GmbH
Werner-Heisenberg-Straße 2
63263 Neu-Isenburg
Germany

Phone: +49 (0)6102 88468-0
Fax: +49 (0)6102 88468-28

http://www.valtech.de

Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672

AW: [RESOLVED] How to add inline javascript in application phase?

Posted by Carsten Kaiser <ca...@valtech.de>.
Ok, I will.

Regards,
CAK



-- 
Carsten Kaiser
Principal Consultant
mailto:carsten.kaiser@valtech.de
Mobile: +49 (0)170 5270206

Valtech GmbH
Werner-Heisenberg-Straße 2
63263 Neu-Isenburg
Germany

Phone: +49 (0)6102 88468-0
Fax: +49 (0)6102 88468-28

http://www.valtech.de

Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672




-----Ursprüngliche Nachricht-----

Von: Simon Kitching [mailto:simon.kitching@rhe.co.nz]
Gesendet: Donnerstag, 29. März 2007 02:00
An: MyFaces Discussion
Betreff: Re: [RESOLVED] How to add inline javascript in application phase?

Ahh..yes, that looks right. StreamingAddResource does try to write it
immediately (and therefore doesn't really support that ResourcePosition
parameter). And that means that it cannot be used from the
invoke-application phase to register stuff to be output later.

Perhaps you could update the wiki with that info?

Carsten Kaiser wrote:
> Ok, I guess I figured out the reason, which causes the solution 
> suggested on the Wiki page to fail:
> In our web application class StreamingAddResource is configured to be 
> used to add resources like JavaScript, CSS etc. to the response, which 
> attempts to write any resource immediately to the response writer. The 
> DefaultAddResource class instead holds a set for each supported resource 
> position wherein all added resources are kept until they are rendered on 
> completion of the render response phase. So later implementation only 
> allows to control popup triggering within the invoke application phase!
>
> Regards,
> CAK
>
> -----Ursprüngliche Nachricht-----
> Von: Carsten Kaiser [mailto:carsten.kaiser@valtech.de]
> Gesendet: Mittwoch, 28. März 2007 06:10
> An: MyFaces Discussion
> Betreff: AW: How to add inline javascript in application phase?
>
> Hello Simon,
>
> thank you very much for his hint. I really could have thought about
> looking for any other value of resource position! I will give it a try!
> Hope, that BODY_ONLOAD will not ask for the ResponseWriter...
>
> Regards,
> CAK
>
>
>

Re: [RESOLVED] How to add inline javascript in application phase?

Posted by Simon Kitching <si...@rhe.co.nz>.
Ahh..yes, that looks right. StreamingAddResource does try to write it 
immediately (and therefore doesn't really support that ResourcePosition 
parameter). And that means that it cannot be used from the 
invoke-application phase to register stuff to be output later.

Perhaps you could update the wiki with that info?

Carsten Kaiser wrote:
> Ok, I guess I figured out the reason, which causes the solution suggested on the Wiki page to fail:
> In our web application class StreamingAddResource is configured to be used to add resources like JavaScript, CSS etc. to the response, which attempts to write any resource immediately to the response writer. The DefaultAddResource class instead holds a set for each supported resource position wherein all added resources are kept until they are rendered on completion of the render response phase. So later implementation only allows to control popup triggering within the invoke application phase!
> 
> Regards,
> CAK  
> 
> -----Ursprüngliche Nachricht-----
> Von: Carsten Kaiser [mailto:carsten.kaiser@valtech.de] 
> Gesendet: Mittwoch, 28. März 2007 06:10
> An: MyFaces Discussion
> Betreff: AW: How to add inline javascript in application phase?
> 
> Hello Simon,
> 
> thank you very much for his hint. I really could have thought about 
> looking for any other value of resource position! I will give it a try! 
> Hope, that BODY_ONLOAD will not ask for the ResponseWriter...
> 
> Regards,
> CAK
> 
> 
> 


[RESOLVED] How to add inline javascript in application phase?

Posted by Carsten Kaiser <ca...@valtech.de>.
Ok, I guess I figured out the reason, which causes the solution suggested on the Wiki page to fail:
In our web application class StreamingAddResource is configured to be used to add resources like JavaScript, CSS etc. to the response, which attempts to write any resource immediately to the response writer. The DefaultAddResource class instead holds a set for each supported resource position wherein all added resources are kept until they are rendered on completion of the render response phase. So later implementation only allows to control popup triggering within the invoke application phase!

Regards,
CAK  

-----Ursprüngliche Nachricht-----
Von: Carsten Kaiser [mailto:carsten.kaiser@valtech.de] 
Gesendet: Mittwoch, 28. März 2007 06:10
An: MyFaces Discussion
Betreff: AW: How to add inline javascript in application phase?

Hello Simon,

thank you very much for his hint. I really could have thought about 
looking for any other value of resource position! I will give it a try! 
Hope, that BODY_ONLOAD will not ask for the ResponseWriter...

Regards,
CAK



-- 
Carsten Kaiser
Principal Consultant
mailto:carsten.kaiser@valtech.de
Mobile: +49 (0)170 5270206

Valtech GmbH
Werner-Heisenberg-Straße 2
63263 Neu-Isenburg
Germany

Phone: +49 (0)6102 88468-0
Fax: +49 (0)6102 88468-28

http://www.valtech.de

Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672




-----Ursprüngliche Nachricht-----

Von: Simon Kitching [mailto:simon.kitching@rhe.co.nz]
Gesendet: Mittwoch, 28. März 2007 05:31
An: MyFaces Discussion
Betreff: Re: How to add inline javascript in application phase?

Mike Kienenberger wrote:
> On 3/27/07, Carsten Kaiser <ca...@valtech.de> wrote:
>> But I'm still interested in getting some statement concerning the
>> description on the Wiki page referenced above.
>
> Edit it :-)

I think Carsten is asking for an explanation of the example code.

After looking at it, I'm not sure why Carsten was getting the original
exception re no response-writer. I hadn't read the code carefully enough
and was assuming that AddResource.addJavascriptInline was writing it out
immediately. However the method takes a "ResourcePosition" parameter of
value HEAD_BEGIN, and is simply adding that inline script to a set of
values to output later. So I'm not sure why the response stream would be
needed then.

By the way, Carsten, the resource position can also be BODY_ONLOAD which
is what you stated you wanted..

Regards,

Simon

AW: How to add inline javascript in application phase?

Posted by Carsten Kaiser <ca...@valtech.de>.
Hello Simon,

thank you very much for his hint. I really could have thought about 
looking for any other value of resource position! I will give it a try! 
Hope, that BODY_ONLOAD will not ask for the ResponseWriter...

Regards,
CAK



-- 
Carsten Kaiser
Principal Consultant
mailto:carsten.kaiser@valtech.de
Mobile: +49 (0)170 5270206

Valtech GmbH
Werner-Heisenberg-Straße 2
63263 Neu-Isenburg
Germany

Phone: +49 (0)6102 88468-0
Fax: +49 (0)6102 88468-28

http://www.valtech.de

Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672




-----Ursprüngliche Nachricht-----

Von: Simon Kitching [mailto:simon.kitching@rhe.co.nz]
Gesendet: Mittwoch, 28. März 2007 05:31
An: MyFaces Discussion
Betreff: Re: How to add inline javascript in application phase?

Mike Kienenberger wrote:
> On 3/27/07, Carsten Kaiser <ca...@valtech.de> wrote:
>> But I'm still interested in getting some statement concerning the
>> description on the Wiki page referenced above.
>
> Edit it :-)

I think Carsten is asking for an explanation of the example code.

After looking at it, I'm not sure why Carsten was getting the original
exception re no response-writer. I hadn't read the code carefully enough
and was assuming that AddResource.addJavascriptInline was writing it out
immediately. However the method takes a "ResourcePosition" parameter of
value HEAD_BEGIN, and is simply adding that inline script to a set of
values to output later. So I'm not sure why the response stream would be
needed then.

By the way, Carsten, the resource position can also be BODY_ONLOAD which
is what you stated you wanted..

Regards,

Simon

Re: How to add inline javascript in application phase?

Posted by Simon Kitching <si...@rhe.co.nz>.
Mike Kienenberger wrote:
> On 3/27/07, Carsten Kaiser <ca...@valtech.de> wrote:
>> But I'm still interested in getting some statement concerning the
>> description on the Wiki page referenced above.
> 
> Edit it :-)

I think Carsten is asking for an explanation of the example code.

After looking at it, I'm not sure why Carsten was getting the original 
exception re no response-writer. I hadn't read the code carefully enough 
and was assuming that AddResource.addJavascriptInline was writing it out 
immediately. However the method takes a "ResourcePosition" parameter of 
value HEAD_BEGIN, and is simply adding that inline script to a set of 
values to output later. So I'm not sure why the response stream would be 
needed then.

By the way, Carsten, the resource position can also be BODY_ONLOAD which 
is what you stated you wanted..

Regards,

Simon


Re: How to add inline javascript in application phase?

Posted by Mike Kienenberger <mk...@gmail.com>.
On 3/27/07, Carsten Kaiser <ca...@valtech.de> wrote:
> But I'm still interested in getting some statement concerning the
> description on the Wiki page referenced above.

Edit it :-)

AW: How to add inline javascript in application phase?

Posted by Carsten Kaiser <ca...@valtech.de>.
Hello Simon,

Thank you for your thoughts. When I interpreted the description stated
on http://wiki.apache.org/myfaces/JavascriptOpenNewWindow correctly
, I would expect this inline javascript code to appear in the 'onload' 
event of the body tag on the resulting page, so that the popup is rendered 
as soon as the page is loaded by the browser. So for me the code fragment 
does not look like an example for a custom renderer class, but maybe I 
misunderstood it.

Nevertheless your suggestion is a feasible workaround to solve my problem.

But I'm still interested in getting some statement concerning the 
description on the Wiki page referenced above.

Regards,
CAK



-- 
Carsten Kaiser
Principal Consultant
mailto:carsten.kaiser@valtech.de
Mobile: +49 (0)170 5270206

Valtech GmbH
Werner-Heisenberg-Straße 2
63263 Neu-Isenburg
Germany

Phone: +49 (0)6102 88468-0
Fax: +49 (0)6102 88468-28

http://www.valtech.de

Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672




-----Ursprüngliche Nachricht-----

Von: Simon Kitching [mailto:simon.kitching@rhe.co.nz]
Gesendet: Dienstag, 27. März 2007 21:58
An: MyFaces Discussion
Betreff: Re: How to add inline javascript in application phase?

Carsten Kaiser wrote:
> Hello,
>
>
>
> I'm facing the following problem:
>
> I have an action, in which I want to trigger a popup window, in case the
> triggered background job failed. According to the MyFaces Wiki
>
> this can be achieved by
>
>
>
>         FacesContext context = getFacesContext();
>
>         ViewHandler viewHandler = getApplication().getViewHandler();
>
>         String actionUrl = viewHandler.getActionURL(context, a_viewId);
>
>         StringBuffer javaScriptText = *new* 
> StringBuffer("window.open('");
>
>         javaScriptText.append(actionUrl);
>
>         javaScriptText.append("', 'popupWindow', 'dependent=yes,
> menubar=no, toolbar=no');");
>
>         AddResource addResource = 
> AddResourceFactory./getInstance/(context);
>
>         addResource.addInlineScriptAtPosition(context,
> AddResource./HEADER_BEGIN/, javaScriptText.toString());
>
>
>
> Unfortunately the ResponseWriter is not yet set on the FacesContext
> during this phase, so a NPE occurs when adding the inline script code
>
> like in the last code line shown above. As far as I figured out the
> response writer is setup/initialized in the render response phase. So
> how could
>
> this code ever worked? Trying to create a response writer manually just
> messed up the whole view... (Later works for the AJAX stuff, but I
>
> guess, that is because the normal response isn't rendered in this case!)
>
>
>
> Any hints welcome!

I don't understand what you're trying to achieve here. It doesn't make
any sense to output javascript "inline" during the invoke-application
phase; where would you expect the javascript to appear in the resulting
page??

What is wrong with doing the following?

<t:div style="display:hidden" rendered="#{bean.popupWanted}">
   <script type="text/javascript">
      // your code to pop up the window
   </script>
</t:div>

where method getPopupWanted returns true if the background job failed.

I expect the code fragment you display above is meant to be used by
custom Renderer classes, not backing beans.

Regards,

Simon

Re: How to add inline javascript in application phase?

Posted by Simon Kitching <si...@rhe.co.nz>.
Carsten Kaiser wrote:
> Hello,
> 
>  
> 
> I’m facing the following problem:
> 
> I have an action, in which I want to trigger a popup window, in case the 
> triggered background job failed. According to the MyFaces Wiki
> 
> this can be achieved by
> 
>  
> 
>         FacesContext context = getFacesContext();
> 
>         ViewHandler viewHandler = getApplication().getViewHandler();
> 
>         String actionUrl = viewHandler.getActionURL(context, a_viewId);
> 
>         StringBuffer javaScriptText = *new* StringBuffer("window.open('");
> 
>         javaScriptText.append(actionUrl);
> 
>         javaScriptText.append("', 'popupWindow', 'dependent=yes, 
> menubar=no, toolbar=no');");
> 
>         AddResource addResource = AddResourceFactory./getInstance/(context);
> 
>         addResource.addInlineScriptAtPosition(context, 
> AddResource./HEADER_BEGIN/, javaScriptText.toString());        
> 
>  
> 
> Unfortunately the ResponseWriter is not yet set on the FacesContext 
> during this phase, so a NPE occurs when adding the inline script code
> 
> like in the last code line shown above. As far as I figured out the 
> response writer is setup/initialized in the render response phase. So 
> how could
> 
> this code ever worked? Trying to create a response writer manually just 
> messed up the whole view… (Later works for the AJAX stuff, but I
> 
> guess, that is because the normal response isn’t rendered in this case!)
> 
>  
> 
> Any hints welcome!

I don't understand what you're trying to achieve here. It doesn't make 
any sense to output javascript "inline" during the invoke-application 
phase; where would you expect the javascript to appear in the resulting 
page??

What is wrong with doing the following?

<t:div style="display:hidden" rendered="#{bean.popupWanted}">
   <script type="text/javascript">
      // your code to pop up the window
   </script>
</t:div>

where method getPopupWanted returns true if the background job failed.

I expect the code fragment you display above is meant to be used by 
custom Renderer classes, not backing beans.

Regards,

Simon