Reputation: 676
I've searched all the web looking for an answer but could not fidn it. I hope someone was dealing with same issue.
I am developing application based on Spring MVC (3.1) and Freemarker (2.3.16). My Freemarker config looks like this:
<!-- FreeMarker parsing -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF" />
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">UTF-8</prop>
<prop key="output_encoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/views/" />
<property name="suffix" value=".ftl" />
<property name="requestContextAttribute" value="rc"></property>
<!-- if you want to use the Spring FreeMarker macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
It's pretty simple. I have no problem with rendering layout/views. The problem is with Spring Controller and writing Request Parameters into view. My simplest possible controller action is like that:
@RequestMapping(value={"/simplest/action","/simplest"}, method=RequestMethod.GET)
@Transactional
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelAndView mv = new ModelAndView("/simplest/action");
mv.addObject("myCustomIdFromView", "66666" );
return mv;
}
View is rendered successfully. In this view I use such construction:
<input type="hidden" name="myCustomIdFromView" id="myCustomIdFromView" value="${myCustomIdFromView}" />
But with such construction Freemarker shows no value. If I change my input to:
<input type="hidden" name="myCustomIdFromView" id="myCustomIdFromView" value="${myCustomIdFromView!'default'}" />
Then 'default' is rendered correctly. So I switched to RequestParameters. And another strange thing. With constructions:
{$RequestParameters.myCustomIdFromView}
{$RequestParameters['myCustomIdFromView']}
I receive empty values of Freemarker 'undefined'.
I've finally came to such solution.
<#assign myCustomIdFromView = '' />
<#list RequestParameters?keys as key>
<#if key == 'myCustomIdFromView'>
<#assign myCustomIdFromView = RequestParameters[key] />
${myCustomIdFromView}
</#if>
</#list>
And IT'S WORKING! Can anyone tell me why I have problems with such simple thing like passing argument to ModelAndView and rendering it in a template? The values are (as You can see) in Model/Request Parameters but it's Freemarker that causes the problem? Any help appreciated.
Cheers, Chlebik
Upvotes: 1
Views: 7990
Reputation: 676
I found out what was wrong. I am using Spring MVC. Therefore there's default rule that says, that all -servlet.xml
config files would be loaded by default.
In my frontcontroller-servlet.xml
I had import statement, which included base applicationContext.xml
. In this file I have another 3 imports (to support defragmentation of config files - with config for DB, controllers, etc).
But what wa also provided in web.xml was:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*</param-value>
</context-param>
And the result of these two things combined was double-creation of standard Spring beans. It came out when I started to integrate with Spring Security - suddenly my application was not able to be deployed - and exceptions were pointing out to double beans existing (@Autowire
annotation went crazy).
So I assume that deep inside Freemarker
classes for Spring (or in Spring itself) something happened and two models existed (with request params). Right now - when I use code from my question everything is rendering right.
Upvotes: 1