Tech News is a blog created by Wasim Akhtar to deliver Technical news with the latest and greatest in the world of technology. We provide content in the form of articles, videos, and product reviews.
Sandra Lite 2013 SP4 (19.50)
via FileHippo.com http://www.filehippo.com/download_sandra_lite/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]
How to access Static Fields in Freemarker FTL
If you defined static fields in Java class and want to use the same in Freemarker (FTL) it is not possible to do so as FTL is not aware of Java objects during rendering. In order to make static fields available to FTL, we need to expose them.
One way of exposing static Java fields to freemarker template is to use them through Model. But if one constant has to be used at multiple places then this would not meet our purpose.
Using Static fields in Spring MVC
In order to use Java static fields in Freemarker in Spring MVC based application, following code snippet can be used:
To access following Java class with static fields:
package net.vibhati;
public class ConstantsEx {
public final static String NAME = " HELLO WORLD";
}
Add below code in Spring MVC Controller:
import freemarker.ext.beans.BeansWrapper;
...
...
BeansWrapper w = new BeansWrapper();
TemplateHashModel statics = w.getStaticModels();
model.addAttribute("statics", statics);
...
In FTL, use JAVA constants as:
${statics["net.vibhati.ConstantsEx"].NAME}
The TemplateHashModel returned from BeanWrapper.getStaticModels, Exposes all static methods and static fields(both final and non-final, using reflection). Now we can add this hashmodel to our Model, as illustrated above. And use it directly in FTL then.
An alternate way to do this is to prepare the TemplateHashModel in a custom filter or Spring Interceptors, so that it becomes common to all the requests(or to the url pattern specified in our custom filter).
This can be implemented is Spring in almost the same manner as explained below for Struts.
Using Static fields in Struts
As MODEL is not available,
In a custom filter, we put our TemplateHashModel in request (this custom filter should be before the Struts 2 filter):
The Java constant class that we need to access in FTL:
package net.vibhati;
public class ConstantsEx {
public static final String NAME = "Hello World";
}
Just make a custom filter(in this case, MyFilter.java) and put our logic in doFilter():
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
BeansWrapper bw = StrutsBeanWrapper.getDefaultInstance();
TemplateHashModel statics = bw.getStaticModels();
request.setAttribute("statics", statics);
chain.doFilter(request, response);
}
In FTL, now we can use “statics” as below:
${statics["net.vibhati.ConstantsEx"].NAME}
How to configure our custom filter in web.xml:
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>net.vibhati.interceptor.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Instead of going all the way and configuring our own filter, we can use OGNL in struts , and get the work done by adding just 1 line in our ftl –
${stack.findValue("@net.vibhati.ConstantsEx@NAME") }
Although, All these approaches do have some security threats associated, as we are exposing all static fields to FTL.
Dig in more , to know more
Refer: http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html
Related Posts
- Struts2 + FreeMarker Tempalte (FTL) Integration example
- Spring MVC + FreeMarker (FTL) Integration example
- FreeMarker Servlet Tutorial with Example
- FreeMarker (FTL) Hello World Tutorial with Example
- Introduction to FreeMarker Template (FTL)
- How To Iterate HashMap in FreeMarker (FTL)
- Spring 3 MVC – Introduction to Spring 3 MVC Framework
via ViralPatel.net http://feedproxy.google.com/~r/viralpatelnet/~3/dkyB_b7LpHI/
[Software Update] Opera 129.0.5823.44 Stable Released, Here is What’s New and Fixed
UPDATE: Release of Opera 129.0.5823.44 stable version to public. Good news for Opera users! Opera team has released new Opera 129.0.5823.44 ...
-
UPDATE: Direct download links added for the latest Mozilla Firefox 131.0.2, 115.16.1 ESR and 128.3.1 ESR offline installers. NOTE: The downl...
-
Newer versions of Windows 11 come with a new security feature called “Windows Protected Print Mode (WPP)“. This article will help you in act...