You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris Rijk <ch...@gmail.com> on 2011/11/10 18:20:35 UTC

Dynamically setting the template directory in an Action

Hi all,

Referring to this page:
http://struts.apache.org/2.2.1/docs/selecting-template-directory.html

It says the template directory can be specified in the session, for example to allow the user to change it on a per-session basis. I've been trying to do this for my application, but can't get it to work.

I'm using the conventions library. If I copy WEB-INF/content to WEB-INF/content2 I can add a tag like @ResultPath(value="/WEB-INF/content2/") to an Action to change the template directory used to render the results, and this works as expected. However, that works on a per action/class basis, and I want to do it on a per session basis.

I've tried setting templateDir in the session but this has no effect. Any ideas?

For example, I've tried this in an action implementing the ServletRequestAware interface:

    public void setServletRequest(HttpServletRequest arg0) {
        HttpSession sess = arg0.getSession(true);
        sess.setAttribute("templateDir", "/WEB-INF/content2/");
    }

I also tried implementing SessionAware, eg:
    public void setSession(Map<String, Object> session) {
        session.put("templateDir", "/WEB-INF/content2/");
    }
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically setting the template directory in an Action

Posted by Josep García <jg...@isigma.es>.
You can try by overriding the default FreemarkerManager, by defining your
own in struts.xml:

    <bean type="org.apache.struts2.views.freemarker.FreemarkerManager"
name="dynamic" class="com.mycompany.MyFreeMarkerManager" />
    <constant name="struts.freemarker.manager.classname" value="dynamic"/>

and override the method:
    protected TemplateLoader getTemplateLoader(ServletContext
servletContext)

Let us know if it works, as I might go for the same direction!

Cheers,
Josep

2011/12/8 Gurcharan Singh <gu...@gmail.com>

> In the same line, I've a question - How to setup Struts2 to use templates
> which are located outside document root directory. Why we want to do it -
> We want to keep UI and Dev work separate and in-case any change is made in
> the template, we shouldn't be re-building the whole app.
>
> Thanks
> Gurcharan
>
> On Mon, Nov 21, 2011 at 1:19 PM, Josep García <jg...@isigma.es> wrote:
>
> > What I have achieved is to set the template Dir for freemarker result
> type,
> > in web.xml:
> >
> >    <!-- struts/freemarker additional template loading: folder -->
> >    <context-param>
> >        <param-name>templatePath</param-name>
> >        <param-value>${form.templateDir}</param-value>
> >    </context-param>
> >
> > with ${form.templateDir} being replaced by maven at compile or packaging
> > phases.
> >
> > Cheers,
> > Josep
> >
> >
> > 2011/11/21 Li Ying <li...@gmail.com>
> >
> > > (1)I believe the word [template] in struts2 means,
> > > the template file to render a struts2 tag.
> > > for example, the file
> > >  [struts2-core-2.2.3.jar]/template/xhtml/form.ftl
> > > is a template file to render the tag <s:form> using theme "xhtml"
> > >
> > > (2)what you are trying to do, is to change the jsp file location which
> > > will be used to render the action result. This is different to the
> > > [template].
> > > maybe you can use [Parameters in action result definitions], described
> > > by the following document:
> > >
> >
> http://struts.apache.org/2.x/docs/parameters-in-configuration-results.html
> > >
> > >
> > > 2011/11/11 Chris Rijk <ch...@gmail.com>:
> > > > Hi all,
> > > >
> > > > Referring to this page:
> > > >
> http://struts.apache.org/2.2.1/docs/selecting-template-directory.html
> > > >
> > > > It says the template directory can be specified in the session, for
> > > example to allow the user to change it on a per-session basis. I've
> been
> > > trying to do this for my application, but can't get it to work.
> > > >
> > > > I'm using the conventions library. If I copy WEB-INF/content to
> > > WEB-INF/content2 I can add a tag like
> > > @ResultPath(value="/WEB-INF/content2/") to an Action to change the
> > template
> > > directory used to render the results, and this works as expected.
> > However,
> > > that works on a per action/class basis, and I want to do it on a per
> > > session basis.
> > > >
> > > > I've tried setting templateDir in the session but this has no effect.
> > > Any ideas?
> > > >
> > > > For example, I've tried this in an action implementing the
> > > ServletRequestAware interface:
> > > >
> > > >    public void setServletRequest(HttpServletRequest arg0) {
> > > >        HttpSession sess = arg0.getSession(true);
> > > >        sess.setAttribute("templateDir", "/WEB-INF/content2/");
> > > >    }
> > > >
> > > > I also tried implementing SessionAware, eg:
> > > >    public void setSession(Map<String, Object> session) {
> > > >        session.put("templateDir", "/WEB-INF/content2/");
> > > >    }
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > > For additional commands, e-mail: user-help@struts.apache.org
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
>

Re: Dynamically setting the template directory in an Action

Posted by Gurcharan Singh <gu...@rachna.co.in>.
Thanks for the clarification. Will sure give it a try.

On Tue, Dec 13, 2011 at 9:04 AM, Li Ying <li...@gmail.com> wrote:

> I mean:
> If the OS is unix/linux, you can create file link (or folder link) on
> the file system.
>
> This doc describes more details:
> http://www.cyberciti.biz/faq/creating-hard-links-with-ln-command/
>
>
> 2011/12/13 Gurcharan Singh <gu...@rachna.co.in>:
> > Hi Li
> >
> > Sorry, not able to understand your point. Can you kindly elaborate.
> >
> > Thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Dynamically setting the template directory in an Action

Posted by Li Ying <li...@gmail.com>.
I mean:
If the OS is unix/linux, you can create file link (or folder link) on
the file system.

This doc describes more details:
http://www.cyberciti.biz/faq/creating-hard-links-with-ln-command/


2011/12/13 Gurcharan Singh <gu...@rachna.co.in>:
> Hi Li
>
> Sorry, not able to understand your point. Can you kindly elaborate.
>
> Thanks!

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


Re: Dynamically setting the template directory in an Action

Posted by Gurcharan Singh <gu...@rachna.co.in>.
Hi Li

Sorry, not able to understand your point. Can you kindly elaborate.

Thanks!


On Tue, Dec 13, 2011 at 8:13 AM, Li Ying <li...@gmail.com> wrote:

> My suggestion:
>
> You can create a file(or folder) link on your file system.
>
> From the view of your web app,
> it works like a file(or folder) inside the [document root directory],
> but the real file(or folder) is actually somewhere outside the
> [document root directory].
>
>
> 2011/12/8 Gurcharan Singh <gu...@gmail.com>:
> > In the same line, I've a question - How to setup Struts2 to use templates
> > which are located outside document root directory. Why we want to do it -
> > We want to keep UI and Dev work separate and in-case any change is made
> in
> > the template, we shouldn't be re-building the whole app.
> >
> > Thanks
> > Gurcharan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Dynamically setting the template directory in an Action

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

You can create a file(or folder) link on your file system.

>From the view of your web app,
it works like a file(or folder) inside the [document root directory],
but the real file(or folder) is actually somewhere outside the
[document root directory].


2011/12/8 Gurcharan Singh <gu...@gmail.com>:
> In the same line, I've a question - How to setup Struts2 to use templates
> which are located outside document root directory. Why we want to do it -
> We want to keep UI and Dev work separate and in-case any change is made in
> the template, we shouldn't be re-building the whole app.
>
> Thanks
> Gurcharan

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


Re: Dynamically setting the template directory in an Action

Posted by Gurcharan Singh <gu...@gmail.com>.
In the same line, I've a question - How to setup Struts2 to use templates
which are located outside document root directory. Why we want to do it -
We want to keep UI and Dev work separate and in-case any change is made in
the template, we shouldn't be re-building the whole app.

Thanks
Gurcharan

On Mon, Nov 21, 2011 at 1:19 PM, Josep García <jg...@isigma.es> wrote:

> What I have achieved is to set the template Dir for freemarker result type,
> in web.xml:
>
>    <!-- struts/freemarker additional template loading: folder -->
>    <context-param>
>        <param-name>templatePath</param-name>
>        <param-value>${form.templateDir}</param-value>
>    </context-param>
>
> with ${form.templateDir} being replaced by maven at compile or packaging
> phases.
>
> Cheers,
> Josep
>
>
> 2011/11/21 Li Ying <li...@gmail.com>
>
> > (1)I believe the word [template] in struts2 means,
> > the template file to render a struts2 tag.
> > for example, the file
> >  [struts2-core-2.2.3.jar]/template/xhtml/form.ftl
> > is a template file to render the tag <s:form> using theme "xhtml"
> >
> > (2)what you are trying to do, is to change the jsp file location which
> > will be used to render the action result. This is different to the
> > [template].
> > maybe you can use [Parameters in action result definitions], described
> > by the following document:
> >
> http://struts.apache.org/2.x/docs/parameters-in-configuration-results.html
> >
> >
> > 2011/11/11 Chris Rijk <ch...@gmail.com>:
> > > Hi all,
> > >
> > > Referring to this page:
> > > http://struts.apache.org/2.2.1/docs/selecting-template-directory.html
> > >
> > > It says the template directory can be specified in the session, for
> > example to allow the user to change it on a per-session basis. I've been
> > trying to do this for my application, but can't get it to work.
> > >
> > > I'm using the conventions library. If I copy WEB-INF/content to
> > WEB-INF/content2 I can add a tag like
> > @ResultPath(value="/WEB-INF/content2/") to an Action to change the
> template
> > directory used to render the results, and this works as expected.
> However,
> > that works on a per action/class basis, and I want to do it on a per
> > session basis.
> > >
> > > I've tried setting templateDir in the session but this has no effect.
> > Any ideas?
> > >
> > > For example, I've tried this in an action implementing the
> > ServletRequestAware interface:
> > >
> > >    public void setServletRequest(HttpServletRequest arg0) {
> > >        HttpSession sess = arg0.getSession(true);
> > >        sess.setAttribute("templateDir", "/WEB-INF/content2/");
> > >    }
> > >
> > > I also tried implementing SessionAware, eg:
> > >    public void setSession(Map<String, Object> session) {
> > >        session.put("templateDir", "/WEB-INF/content2/");
> > >    }
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>

Re: Dynamically setting the template directory in an Action

Posted by Josep García <jg...@isigma.es>.
What I have achieved is to set the template Dir for freemarker result type,
in web.xml:

    <!-- struts/freemarker additional template loading: folder -->
    <context-param>
        <param-name>templatePath</param-name>
        <param-value>${form.templateDir}</param-value>
    </context-param>

with ${form.templateDir} being replaced by maven at compile or packaging
phases.

Cheers,
Josep


2011/11/21 Li Ying <li...@gmail.com>

> (1)I believe the word [template] in struts2 means,
> the template file to render a struts2 tag.
> for example, the file
>  [struts2-core-2.2.3.jar]/template/xhtml/form.ftl
> is a template file to render the tag <s:form> using theme "xhtml"
>
> (2)what you are trying to do, is to change the jsp file location which
> will be used to render the action result. This is different to the
> [template].
> maybe you can use [Parameters in action result definitions], described
> by the following document:
> http://struts.apache.org/2.x/docs/parameters-in-configuration-results.html
>
>
> 2011/11/11 Chris Rijk <ch...@gmail.com>:
> > Hi all,
> >
> > Referring to this page:
> > http://struts.apache.org/2.2.1/docs/selecting-template-directory.html
> >
> > It says the template directory can be specified in the session, for
> example to allow the user to change it on a per-session basis. I've been
> trying to do this for my application, but can't get it to work.
> >
> > I'm using the conventions library. If I copy WEB-INF/content to
> WEB-INF/content2 I can add a tag like
> @ResultPath(value="/WEB-INF/content2/") to an Action to change the template
> directory used to render the results, and this works as expected. However,
> that works on a per action/class basis, and I want to do it on a per
> session basis.
> >
> > I've tried setting templateDir in the session but this has no effect.
> Any ideas?
> >
> > For example, I've tried this in an action implementing the
> ServletRequestAware interface:
> >
> >    public void setServletRequest(HttpServletRequest arg0) {
> >        HttpSession sess = arg0.getSession(true);
> >        sess.setAttribute("templateDir", "/WEB-INF/content2/");
> >    }
> >
> > I also tried implementing SessionAware, eg:
> >    public void setSession(Map<String, Object> session) {
> >        session.put("templateDir", "/WEB-INF/content2/");
> >    }
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Dynamically setting the template directory in an Action

Posted by Li Ying <li...@gmail.com>.
(1)I believe the word [template] in struts2 means,
the template file to render a struts2 tag.
for example, the file
  [struts2-core-2.2.3.jar]/template/xhtml/form.ftl
is a template file to render the tag <s:form> using theme "xhtml"

(2)what you are trying to do, is to change the jsp file location which
will be used to render the action result. This is different to the
[template].
maybe you can use [Parameters in action result definitions], described
by the following document:
http://struts.apache.org/2.x/docs/parameters-in-configuration-results.html


2011/11/11 Chris Rijk <ch...@gmail.com>:
> Hi all,
>
> Referring to this page:
> http://struts.apache.org/2.2.1/docs/selecting-template-directory.html
>
> It says the template directory can be specified in the session, for example to allow the user to change it on a per-session basis. I've been trying to do this for my application, but can't get it to work.
>
> I'm using the conventions library. If I copy WEB-INF/content to WEB-INF/content2 I can add a tag like @ResultPath(value="/WEB-INF/content2/") to an Action to change the template directory used to render the results, and this works as expected. However, that works on a per action/class basis, and I want to do it on a per session basis.
>
> I've tried setting templateDir in the session but this has no effect. Any ideas?
>
> For example, I've tried this in an action implementing the ServletRequestAware interface:
>
>    public void setServletRequest(HttpServletRequest arg0) {
>        HttpSession sess = arg0.getSession(true);
>        sess.setAttribute("templateDir", "/WEB-INF/content2/");
>    }
>
> I also tried implementing SessionAware, eg:
>    public void setSession(Map<String, Object> session) {
>        session.put("templateDir", "/WEB-INF/content2/");
>    }
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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