using freemarker variables in the application context so that it is used in the ftl(free marker code)

982 views
Skip to first unread message

Shrileckha Chaithanya

unread,
Oct 2, 2014, 5:42:08 PM10/2/14
to hippo-c...@googlegroups.com
Hi Hippo team,

I want to pass the global free marker variables from the application context so that it is can be used in the free marker.

 <!-- Freemarker config the free marker variables can be used in the free marker-->
  <bean  id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPaths" value="file:///web/gcom/app/hippo/templates/view/ftl/events/base/" />
     
      <!--  This should be set per environment or retrieved from a database table -->
      <property name="freemarkerVariables">
          <map>
            <entry key="www" value="testing"/>
          </map>
        </property>
        <property name="freemarkerSettings">
        <props>
            <prop key="default_encoding">UTF-8</prop>
            <prop key="auto_import">spring.ftl as spring</prop>
        </props>
        </property>
   </bean>

I have used this bean to do that job. It hooks the global variable www from context to the free marker.
here is an example of how to use the free marker variables.
http://stackoverflow.com/questions/3129167/first-steps-with-freemarker-with-spring-mvc

I tried to plug in the above piece of code for the hippo app, it is not working. Can you pls give me any insight of how to go about it?

Jeroen Reijn

unread,
Oct 3, 2014, 4:55:22 AM10/3/14
to hippo-c...@googlegroups.com
Hi,

the code snippet is indeed for configuring the freemarker servlet within Spring itself. Now by default Hippo has a FreemarkerServlet defined within the web.xml of the project.

Have you changed this to instantiate it from Spring or are you trying to achieve something similar with Hippo as you would with Spring?

Jeroen

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--
Jeroen Reijn
Hippo

Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 101 Main Street, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com

http://about.me/jeroenreijn

Shrileckha Chaithanya

unread,
Oct 3, 2014, 10:13:07 AM10/3/14
to hippo-c...@googlegroups.com
Hi Jeroen,

I am to achieve something similar with Hippo as I did with Spring.

I have this config in my web.xml for free marker.

 <servlet>
    <servlet-name>freemarker</servlet-name>
    <servlet-class>org.hippoecm.hst.servlet.HstFreemarkerServlet</servlet-class>
    <!-- FreemarkerServlet settings: -->
    <init-param>
      <param-name>TemplatePath</param-name>
      <param-value>file:///web/gcom/app/hippo/templates/view/</param-value>
    </init-param>
    <init-param>
      <param-name>ContentType</param-name>
      <param-value>text/html; charset=UTF-8</param-value>
      <!-- Forces UTF-8 output encoding! -->
    </init-param>
    <!--
      if in case of template exception rendering should short-circuit in case of errors, set template_exception_handler = debug
      Default template_exception_handler is "ignore" which only logs errors and then continues rendering
      <init-param>
        <param-name>template_exception_handler</param-name>
        <param-value>debug</param-value>
      </init-param>
    -->
    <load-on-startup>200</load-on-startup>
  </servlet>

Any help would be appreciated.

Thanks,
Shrileckha Chaithanya

You received this message because you are subscribed to a topic in the Google Groups "Hippo Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hippo-communi...@googlegroups.com.

Minos Chatzidakis

unread,
Oct 6, 2014, 7:55:51 AM10/6/14
to hippo-c...@googlegroups.com
Hi Shrileckha,

I think I've worked on something similar, but I'm not sure my solution will fit you. In my case i wanted to load configuration stored in the hst mounts.
In general the freemarker.ext.servlet.FreemarkerServlet which is used in Hippo is somewhat limited when it comes to loading configuration per template execution. Problem is, you need to modify these parameters in the environment of the freemarker template, but unfortunately that's not accessible in this default servlet. So the solution I can propose to you is a fork.. I'm not sure you want to do this though. Let me know if you're up for it, then I will proceed with explaining what we did!

Minos
--
With kind regards/Met vriendelijke groet,
Minos Chatzidakis

Woonsan Ko

unread,
Oct 6, 2014, 10:52:05 AM10/6/14
to hippo-c...@googlegroups.com
Hi Shrileckha,

I think you may override #createConfiguration() method from
HstFreemarkerServlet like the following and replace the
HstFreemarkerServlet configuration in web.xml by yours:

@Override
protected Configuration createConfiguration() {
Configuration config = super.createConfiguration();

// set shared variables here, for example:
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("www", "testing");
// ...
config.setAllSharedVariables(new SimpleHash(vars,
config.getObjectWrapper()));

return config;
}

HTH,

Woonsan
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the
> Google Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to
> hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> Jeroen Reijn
> Hippo
>
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> Boston - 101 Main Street, Cambridge, MA 02142
>
> US +1 877 414 4776 <tel:%2B1%20877%20414%204776> (toll free)
> Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
> www.onehippo.com <http://www.onehippo.com>
>
> http://about.me/jeroenreijn
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to a topic in
> the Google Groups "Hippo Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Hippo Community Group: The place for all discussions and announcements
> about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.


--
w....@onehippo.com www.onehippo.com

Shrileckha Chaithanya

unread,
Oct 6, 2014, 10:52:06 AM10/6/14
to hippo-c...@googlegroups.com
Hi Minos,

yes I am interested to know. What is your solution? can you pls detail it out.

Thanks,
Shrileckha Chaitanya

Shrileckha Chaithanya

unread,
Oct 6, 2014, 10:56:24 AM10/6/14
to hippo-c...@googlegroups.com
thanks Woonsan.. Minoss also had the same idea?

Btw can you pls explain the step by step I need to do.. Not sure how i can override the  HstFreemarkerServlet. Are you telling me to create a class that extends HstFreemarkerServlet and use it in the web.xml?

To unsubscribe from this group and all its topics, send an email to hippo-communi...@googlegroups.com.

Woonsan Ko

unread,
Oct 6, 2014, 11:04:00 AM10/6/14
to hippo-c...@googlegroups.com
On 10/6/14 10:56 AM, Shrileckha Chaithanya wrote:
> thanks Woonsan.. Minoss also had the same idea?
>
> Btw can you pls explain the step by step I need to do.. Not sure how i
> can override the HstFreemarkerServlet. Are you telling me to create a
> class that extends HstFreemarkerServlet and use it in the web.xml?

Yes, you should add a class which extends HstFreemarkerServlet like the
following:

package org.example.servlet;

public class MyHstFreemarkerServlet extends HstFreemarkerServlet {

@Override
protected Configuration createConfiguration() {
Configuration config = super.createConfiguration();

// set shared variables here, for example:
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("www", "testing");
// ...
// NOTE: You might want to improve the loading vars better
// (e.g, loading from init param and prop file)

config.setAllSharedVariables(new SimpleHash(vars,
config.getObjectWrapper()));

return config;
}
}

And, change servlet classname in the web.xml like this:


<servlet-class>org.example.servlet.MyHstFreemarkerServlet</servlet-class>


Woonsan
> > <mailto:j.r...@onehippo.com <mailto:j.r...@onehippo.com>>> wrote:
> >
> > Hi,
> >
> > the code snippet is indeed for configuring the freemarker servlet
> > within Spring itself. Now by default Hippo has a FreemarkerServlet
> > defined within the web.xml of the project.
> >
> > Have you changed this to instantiate it from Spring or are you
> > trying to achieve something similar with Hippo as you would with Spring?
> >
> > Jeroen
> >
> > On Thu, Oct 2, 2014 at 11:42 PM, Shrileckha Chaithanya
> > <cshri...@gmail.com <mailto:cshri...@gmail.com>
> > <mailto:hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>>
> > RSS:
> > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > ---
> > You received this message because you are subscribed to the
> > Google Groups "Hippo Community" group.
> > To unsubscribe from this group and stop receiving emails from
> > it, send an email to
> > hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>>.
> > Visit this group at http://groups.google.com/group/hippo-community.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> >
> >
> > --
> > Jeroen Reijn
> > Hippo
> >
> > Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> > Boston - 101 Main Street, Cambridge, MA 02142
> >
> > US +1 877 414 4776 <tel:%2B1%20877%20414%204776>
> <tel:%2B1%20877%20414%204776> (toll free)
> > Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
> <tel:%2B31%280%2920%20522%204466>
> > www.onehippo.com <http://www.onehippo.com>
> <http://www.onehippo.com>
> >
> > http://about.me/jeroenreijn
> >
> > --
> > Hippo Community Group: The place for all discussions and
> > announcements about Hippo CMS (and HST, repository etc. etc.)
> >
> > To post to this group, send email to
> > hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> > <mailto:hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>>
> > RSS:
> > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > ---
> > You received this message because you are subscribed to a topic in
> > the Google Groups "Hippo Community" group.
> > To unsubscribe from this topic, visit
> > https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to
> > hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>>.
> > Visit this group at http://groups.google.com/group/hippo-community.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> > --
> > Hippo Community Group: The place for all discussions and announcements
> > about Hippo CMS (and HST, repository etc. etc.)
> >
> > To post to this group, send email to hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> > RSS:
> > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > ---
> > You received this message because you are subscribed to the Google
> > Groups "Hippo Community" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>>.
> > Visit this group at http://groups.google.com/group/hippo-community.
> > For more options, visit https://groups.google.com/d/optout.
>
>
> --
> w....@onehippo.com <mailto:w....@onehippo.com> www.onehippo.com
> <http://www.onehippo.com>
> Boston - 101 Main Street, Cambridge, MA 02142
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> US +1 877 414 4776 <tel:%2B1%20877%20414%204776> (toll free)
> Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to a topic in
> the Google Groups "Hippo Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>.

Minos Chatzidakis

unread,
Oct 6, 2014, 11:10:43 AM10/6/14
to hippo-c...@googlegroups.com
On Mon, Oct 6, 2014 at 4:52 PM, Shrileckha Chaithanya <cshri...@gmail.com> wrote:
Hi Minos,

yes I am interested to know. What is your solution? can you pls detail it out.

It was very simple. We just created a method processEnvironment and changed method process() so that it uses it. The code was:

template.process(model, response.getWriter());

and ours is:

Environment environment = template.createProcessingEnvironment(model, response.getWriter());
processEnvironment(request, response, template, model, environment);
environment.process();

So now the processEnvironment() method can be used to set variables in the environment. Just extend from your forked class and override the processEnvironment() method. In the forked class, this method does nothing:

    /**
     * Override this method to modify the Template's environment, just before the Template gets processed
     */
    protected void processEnvironment(final HttpServletRequest request, final HttpServletResponse response, final Template template, final TemplateModel model, final Environment environment) {
    }


HTH!
Minos
--

Woonsan Ko

unread,
Oct 6, 2014, 11:14:40 AM10/6/14
to hippo-c...@googlegroups.com
Just for your information, Minos' solution is good especially when you
have to set variables for the specific requests, not globally.
My solution is in the same approach as how you did with the Spring
Framework for globally shared variables.
So, if you just want to set some globally shared variables for
templates, not request specific, then my solution would be simpler, I'm
afraid.

Regards,

Woonsan
> <cshri...@gmail.com <mailto:cshri...@gmail.com>>
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed
> to the Google Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving
> emails from it, send an email to
> hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at
> http://groups.google.com/group/hippo-community.
> For more options, visit
> https://groups.google.com/d/optout.
>
>
>
>
> --
> Jeroen Reijn
> Hippo
>
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> Boston - 101 Main Street, Cambridge, MA 02142
>
> US +1 877 414 4776 <tel:%2B1%20877%20414%204776> (toll free)
> Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
> www.onehippo.com <http://www.onehippo.com>
>
> http://about.me/jeroenreijn
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc.
> etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to
> a topic in the Google Groups "Hippo Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> To unsubscribe from this group and all its topics, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at
> http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the
> Google Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails
> from it, send an email to
> hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at
> http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> With kind regards/Met vriendelijke groet,
> Minos Chatzidakis
>
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> Boston - 101 Main Street, Cambridge, MA 02142
>
> US +1 877 414 4776 (toll free)
> Europe +31(0)20 522 4466
> www.onehippo.com <http://www.onehippo.com/>
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to a topic
> in the Google Groups "Hippo Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email
> to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to
> hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> With kind regards/Met vriendelijke groet,
> Minos Chatzidakis
>
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> Boston - 101 Main Street, Cambridge, MA 02142
>
> US +1 877 414 4776 (toll free)
> Europe +31(0)20 522 4466
> www.onehippo.com <http://www.onehippo.com/>
>
> --
> Hippo Community Group: The place for all discussions and announcements
> about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.


--
w....@onehippo.com www.onehippo.com

Shrileckha Chaithanya

unread,
Oct 6, 2014, 11:40:49 AM10/6/14
to hippo-c...@googlegroups.com
THanks you it works. I got that part. But in this case, the shared variables are same for all the environment(dev, qa, prod) and it comes from the java class. I want to externalize it in a property file( so they are different for all the environment) and it would be convinient if these values are coming from the web.xml or context file. any idea? I am looking at it right now how can extend the class that extends the HSTServlet. thank you very much for your help so far.

To unsubscribe from this group and all its topics, send an email to hippo-communi...@googlegroups.com.

Shrileckha Chaithanya

unread,
Oct 6, 2014, 3:03:36 PM10/6/14
to hippo-c...@googlegroups.com
I did this to get the variables from the props file.

PropertiesUtil class loads the below env.properties.

I have this in the app context :

    <!-- Loading evn properties -->
  <bean id="envProps" class="xx.xxx.utils.PropertiesUtil">
          <property name="locations">
            <list>
                <value>file:/web/properties/env.properties</value>
            </list>
        </property>
      <!-- Required when using multiple property placeholders; set the last file to false. -->
      <property name="ignoreUnresolvablePlaceholders" value="false"/>
  </bean>

in the custom hst servlet class.


@Override
    protected Configuration createConfiguration() {
        Configuration config = super.createConfiguration();
       
        Map<String, Object> vars = new HashMap<String, Object>();
       
     
            log.info("*******************************The map is empty");
            vars.put("www", PropertiesUtil.getProperty("www"));
            vars.put("www_s", PropertiesUtil.getProperty("www_s"));

       
        // set shared variables here, for example:
       
        try{
            config.setAllSharedVariables(new SimpleHash(vars,config.getObjectWrapper()));
        }catch(Exception e){
            e.printStackTrace();
        }

        return config;
    }

But still I need to make this in the java class and it will be part of the war file.
I want to do this in the application context or the web.xml where the HSTFreemarkerServlet gets loaded.

for eg) like the below one but it is not working.


 
   <!-- Freemarker config the free marker variables can be used in the free marker-->
  <bean id="customHstServlet" class="com.hippo.HSTServlet.CustomHSTServletConfiguration">

  
      <property name="freemarkerVariables">
          <map>
            <entry key="www" value="testing"/>
          </map>
        </property>
      
   </bean>

Any help would be appreciated.

Thanks,

Woonsan Ko

unread,
Oct 6, 2014, 3:22:42 PM10/6/14
to hippo-c...@googlegroups.com
On 10/6/14 3:03 PM, Shrileckha Chaithanya wrote:
> I did this to get the variables from the props file.
>
> PropertiesUtil class loads the below env.properties.
>
> I have this in the app context :
>
> <!-- Loading evn properties -->
> <bean id="envProps" class="xx.xxx.utils.PropertiesUtil">
> <property name="locations">
> <list>
> <value>file:/web/properties/env.properties</value>
> </list>
> </property>
> <!-- Required when using multiple property placeholders; set the
> last file to false. -->
> <property name="ignoreUnresolvablePlaceholders" value="false"/>
> </bean>
>
> in the custom hst servlet class.
>
> @Override
> protected Configuration createConfiguration() {
> Configuration config = super.createConfiguration();
>
> Map<String, Object> vars = new HashMap<String, Object>();
>
>
> log.info
> <http://log.info>("*******************************The map is empty");
> vars.put("www", PropertiesUtil.getProperty("www"));
> vars.put("www_s", PropertiesUtil.getProperty("www_s"));
>
> // set shared variables here, for example:
>
> try{
> config.setAllSharedVariables(new
> SimpleHash(vars,config.getObjectWrapper()));
> }catch(Exception e){
> e.printStackTrace();
> }
>
> return config;
> }
>
> But still I need to make this in the java class and it will be part of
> the war file.
> I want to do this in the application context or the web.xml where the
> HSTFreemarkerServlet gets loaded.

If you want to read the properties from web.xml, then why don't you
replace the example code ('vars.put(...); vars.put(...);') by something
like the following?

String propsParam = getServletConfig().getInitParameter("shared_vars");
// load properties from the propParam. See [1] for detail
Properties props = ...
// iterate the props to put each to vars map

Regards,

Woonsan

[1] http://stackoverflow.com/questions/9672327/parsing-string-as-properties
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>
> > >
> <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>>.
> > > Visit this group at http://groups.google.com/group/hippo-community.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> > >
> > >
> > >
> > > --
> > > Jeroen Reijn
> > > Hippo
> > >
> > > Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> > > Boston - 101 Main Street, Cambridge, MA 02142
> > >
> > > US +1 877 414 4776 <tel:%2B1%20877%20414%204776>
> <tel:%2B1%20877%20414%204776>
> > <tel:%2B1%20877%20414%204776> (toll free)
> > > Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
> <tel:%2B31%280%2920%20522%204466>
> > <tel:%2B31%280%2920%20522%204466>
> > > www.onehippo.com <http://www.onehippo.com> <http://www.onehippo.com>
> > <http://www.onehippo.com>
> > >
> > > http://about.me/jeroenreijn
> > >
> > > --
> > > Hippo Community Group: The place for all discussions and
> > > announcements about Hippo CMS (and HST, repository etc. etc.)
> > >
> > > To post to this group, send email to
> > > hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> > <mailto:hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>>
> <mailto:hippo-c...@googlegroups.com>>>
> > > RSS:
> > > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > > ---
> > > You received this message because you are subscribed to a topic in
> > > the Google Groups "Hippo Community" group.
> > > To unsubscribe from this topic, visit
> > > https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> > > To unsubscribe from this group and all its topics, send an email to
> > > hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>
> > > <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>>.
> > > Visit this group at http://groups.google.com/group/hippo-community.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> > >
> > > --
> > > Hippo Community Group: The place for all discussions and announcements
> > > about Hippo CMS (and HST, repository etc. etc.)
> > >
> > > To post to this group, send email to hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> > <mailto:hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>>
> > > RSS:
> > > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > > ---
> > > You received this message because you are subscribed to the Google
> > > Groups "Hippo Community" group.
> > > To unsubscribe from this group and stop receiving emails from it, send
> > > an email to hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>
> > > <mailto:hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>>.
> > > Visit this group at http://groups.google.com/group/hippo-community.
> > > For more options, visit https://groups.google.com/d/optout.
> >
> >
> > --
> > w....@onehippo.com <mailto:w....@onehippo.com>
> <mailto:w....@onehippo.com <mailto:w....@onehippo.com>>
> www.onehippo.com <http://www.onehippo.com>
> > <http://www.onehippo.com>
> > Boston - 101 Main Street, Cambridge, MA 02142
> > Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> > US +1 877 414 4776 <tel:%2B1%20877%20414%204776>
> <tel:%2B1%20877%20414%204776> (toll free)
> > Europe +31(0)20 522 4466 <tel:%2B31%280%2920%20522%204466>
> <tel:%2B31%280%2920%20522%204466>
> >
> > --
> > Hippo Community Group: The place for all discussions and
> > announcements about Hippo CMS (and HST, repository etc. etc.)
> >
> > To post to this group, send email to
> > hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>
> > <mailto:hippo-c...@googlegroups.com
> <mailto:hippo-c...@googlegroups.com>>
> > RSS:
> > https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> > ---
> > You received this message because you are subscribed to a topic in
> > the Google Groups "Hippo Community" group.
> > To unsubscribe from this topic, visit
> > https://groups.google.com/d/topic/hippo-community/xJRsp9CVFVM/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to
> > hippo-communi...@googlegroups.com
> <mailto:hippo-community%2Bunsu...@googlegroups.com>
> > <mailto:hippo-community%2Bunsu...@googlegroups.com
> <mailto:hippo-community%252Buns...@googlegroups.com>>.

Shrileckha Chaithanya

unread,
Oct 7, 2014, 3:09:09 PM10/7/14
to hippo-c...@googlegroups.com

Can we do something like this suggested by the free marker site? All our other apps work that I worked on work this way and only exception is hippo since it goes via HSTFreeMarkerServlet.

 <!-- Freemarker config -->

   <bean  id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPaths">
        <list>
           <value>${freemarker-template-folder}</value>
           <value>${freemarker-email-template-folder}</value>
           <value>/WEB-INF/lib</value>
        </list>
      </property>

      <!--  This should be set per environment or retrieved from a database table -->
      <property name="freemarkerVariables">
          <map>
            <entry key="www" value="${www}"/>
            <entry key="www_s" value="${www_s}"/>
            <entry key="wwwnoscheme" value="${wwwnoscheme}"/>
            <entry key="imageserver" value="${imageserver}"/>
            <entry key="imageserver_s" value="${imageserver_s}"/>

           
          </map>
        </property>
        <property name="freemarkerSettings">
        <props>
            <prop key="default_encoding">UTF-8</prop>
            <prop key="auto_import">spring.ftl as spring</prop>
        </props>
        </property>
   </bean>

if you see the values for the entry key. it is all coming from the properties file.


To unsubscribe from this group and all its topics, send an email to hippo-communi...@googlegroups.com.

Shrileckha Chaithanya

unread,
Oct 13, 2014, 11:53:48 AM10/13/14
to hippo-c...@googlegroups.com
I was able to solve this issue.

Created a custom class that extends HSTFreeMarker.


 protected Configuration createConfiguration() {
        Configuration config = super.createConfiguration();
       
        try{
          
            // setting the application context.
            setApplicationContext();
           
            Map<String, String>  freeMakerVariables = (LinkedHashMap)context.getBean("messageFactoryMap");
           
            // setting the free marker variables as a shared variables to be used in the ftl.
            if(null!=freeMakerVariables && !freeMakerVariables.isEmpty()){

                // set shared variables here, for example:
                config.setAllSharedVariables(new SimpleHash(freeMakerVariables,config.getObjectWrapper()));
                printMap(freeMakerVariables);
            }else{
                log.info("No shared free marker variables");

            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return config;
    }


 private static void printMap(Map z) {
            for (Object o : z.entrySet()) {
              Map.Entry e = (Map.Entry) o;
              log.info(e.getKey() + " => " + e.getValue().getClass() + " " + e.getValue());
            }
     }

applicationContext.xml

<bean id="envProps" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
            <list>
                <value>file:///xxx/xxxx/env.properties</value>

            </list>
        </property>
      <!-- Required when using multiple property placeholders; set the last file to false. -->
      <property name="ignoreUnresolvablePlaceholders" value="false"/>
  </bean>

 <bean id="messageFactoryMap"  class="org.springframework.beans.factory.config.MapFactoryBean">
    <property name="sourceMap">

        <map>
            <entry key="www" value="${www}"/>
        </map>
    </property>
</bean>


With this I was able to get the values from the property and pass it to the Custom class.

Thanks Hippo team for suggestions.


Reply all
Reply to author
Forward
0 new messages