You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by st...@spamgourmet.com on 2010/11/13 02:47:51 UTC

Running external Javascript files through Struts/Freemarker -- access to ActionContext and ValueStack?

I have an action and freemarker result that includes an external Javascript file.  This javascript files needs to be able to use the getText() method.  I figured I'd name it "util.js.ftl" so that accessing it directly caused the sitemesh-freemarker filter to catch it, but this throws a NullPointerException (maybe because there's no ActionContext?):

GET /myservlet/util.js.ftl
java.lang.NullPointerException
	org.apache.struts2.sitemesh.FreemarkerDecoratorServlet.process(FreemarkerDecoratorServlet.java:176)
	org.apache.struts2.sitemesh.FreemarkerDecoratorServlet.doGet(FreemarkerDecoratorServlet.java:130)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter.doFilter(StrutsExecuteFilter.java:85)
	com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
	com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter.doFilter(StrutsPrepareFilter.java:82)
	rss.web.filter.SkinFilter.doFilter(SkinFilter.java:98)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
The action is simple and just returns SUCCESS.

        <action name="index" class="com.rgm.action.IndexAction">
            <result name="success" type="freemarker">index.ftl</result>
        </action>

Is there a recommended way for a result template to load external files such that they are separate GET requests from the browser, but are still parsed through the Sitemesh-Freemarker filters and servlets, with access to the value stack?  If I simply <#include "*/util.js.ftl" /> then the content is inlined -- this is not what I'm after.

I am using Struts 2.2.1 with Sitemesh+Freemarker Decorators as described on the sitemesh-plugin.html doc in the "Full Integration" section.

Sincerely,
Roland

Re: Running external Javascript files through Struts/Freemarker -- access to ActionContext and ValueStack?

Posted by Li Ying <li...@gmail.com>.
My suggestion:

(1)Reference your js file as a static resource.

(2)If the js need some dynamic information from your action.
You can put js variables or hidden tags in your jsp,
and retrieve these variables or hidden tags in your js.

The js can be static outside resource, but [dynamic information] will
be inlined in your jsp.
This is a little ugly, but i think it is very simple.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Running external Javascript files through Struts/Freemarker -- access to ActionContext and ValueStack?

Posted by Dave Newton <da...@gmail.com>.
If the request isn't hitting an action or the S2 interceptor stack and
filter there will not be a value stack... if you just request an ftl it
won't be processed by S2 at all, no?

Options include making an actual S2 request, passing in messages to the JS,
creating a global message object and referencing it from the JS, and so on.

Dave
On Nov 12, 2010 8:48 PM, <st...@spamgourmet.com> wrote:
> I have an action and freemarker result that includes an external
Javascript file. This javascript files needs to be able to use the getText()
method. I figured I'd name it "util.js.ftl" so that accessing it directly
caused the sitemesh-freemarker filter to catch it, but this throws a
NullPointerException (maybe because there's no ActionContext?):
>
> GET /myservlet/util.js.ftl
> java.lang.NullPointerException
>
org.apache.struts2.sitemesh.FreemarkerDecoratorServlet.process(FreemarkerDecoratorServlet.java:176)
>
org.apache.struts2.sitemesh.FreemarkerDecoratorServlet.doGet(FreemarkerDecoratorServlet.java:130)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
>
org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter.doFilter(StrutsExecuteFilter.java:85)
>
com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
>
com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter.doFilter(StrutsPrepareFilter.java:82)
> rss.web.filter.SkinFilter.doFilter(SkinFilter.java:98)
>
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
> The action is simple and just returns SUCCESS.
>
> <action name="index" class="com.rgm.action.IndexAction">
> <result name="success" type="freemarker">index.ftl</result>
> </action>
>
> Is there a recommended way for a result template to load external files
such that they are separate GET requests from the browser, but are still
parsed through the Sitemesh-Freemarker filters and servlets, with access to
the value stack? If I simply <#include "*/util.js.ftl" /> then the content
is inlined -- this is not what I'm after.
>
> I am using Struts 2.2.1 with Sitemesh+Freemarker Decorators as described
on the sitemesh-plugin.html doc in the "Full Integration" section.
>
> Sincerely,
> Roland