Configuring auto_escape in freemarker spring-mvc Application - spring

I am using spring-mvc with freemarker-2.3.27-incubating.jar.
And bean configuration for view-resolver like below...
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/"/>
<property name="freemarkerSettings">
<props>
<prop key="template_exception_handler">rethrow</prop>
<prop key="number_format">0.########</prop>
<prop key="date_format">dd/MM/yyyy</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
Where and how can I use <#ftl output_format="HTML"> or <#ftl output_format="HTML" auto_esc=true> to enable HTML auto-scape ?

The #ftl tags you just add at the beginning of the template files themselves. But I would recommend setting recognize_standard_file_extensions to true in the freemarkerSettings properties, and then use .ftlh file extension instead of .ftl. Or, even better, you set incompatible_improvements to 2.3.27 there instead, which enables recognize_standard_file_extensions and some fixes. (Last not least, in case you want this for .ftl for some reason, you can set output_format to HTMLOutputFormat in freemarkerSettings properties too.)

Related

Hibernate temporary datastore

I doing unit testing on spring-hibernate DAOs... configured using
#ContextConfiguration(
locations = {
"classpath:test-applicationContext.xml"
})
but it looks like its transacting with the actual database.
How do I use a temporary datastore with out working on actual database
Define your data store on a separate file and include that xml file with your mail application xml. When testing, include a separate xml file to hold your data store pointing to another database such as hsql. Then this will be the data source referred to by your main applicationContext.xml.
Thanks Guys I used H2 and got it working:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url"
value="jdbc:h2:mem:processdb;INIT=RUNSCRIPT FROM 'classpath:create.sql'" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stateDAO" class="com.tutorial.jquery.dao.impl.StateDAOImpl"></bean>
<bean id="stateService" class="com.tutorial.jquery.service.impl.StateServiceImpl"></bean>

Reading Enviornment variables in Spring application context

I have three enviornment variables.
MY_TOPIC
MY_CONTEXT_FACTORY
MY_LDAP
First one - MY_TOPIC works like this
<bean id="myPublishTopic" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
<property name="jndiName">
<value>fxClientDestinationUID=${MY_TOPIC}</value>
</property>
</bean>
Last two does not work reading from enviornment variables. How do it make this work?
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${MY_CONTEXT_FACTORY}</prop>
<prop key="java.naming.provider.url">${MY_LDAP}</prop>
</props>
</property>
</bean>
Last two works only if i read it from properties file which i want to avoid.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/classes/springConfig-devel.properties</value></property>
</bean>
If you are using Spring 3+ you can use Spring Expression Language.
Use #{systemEnvironment['NAME']} to access environment variables
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">#{systemEnvironment['MY_CONTEXT_FACTORY']}</prop>
<prop key="java.naming.provider.url">#{systemEnvironment['MY_LDAP']}</prop>
</props>
</property>
</bean>
Use #{systemProperties['value']} for Java system properties.

Spring - setting property value from JNDI

It is a bit of followup to my previous question
Spring and Hibernate - changing dialect
if for example I have this piece of .xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >
</prop>
</props>
</property>
</bean>
now, I wanted to set hibernate.dialect to value that is exposed by jndi by jdbc/dialect, but when I put <jee:jndi-lookup jndi-name="jdbc/MyDataSource"/> I am getting Invalid content was found starting with element 'jee:jndi-lookup'. No child element is expected at this so I suspect that I can't put any tags in prop.
So, is there any way I can insert jndi resource to this property?
Not completely sure, but you should be able to use Spring-EL here, like this:
<jee:jndi-lookup id="dialect" jndi-name="..." />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >#{dialect}</prop>
</props>
</property>
</bean>

Spring PropertyPlaceholderConfigurer Default Properties Not Read

I am trying to embed activemq broker in a Tomcat. The code base will be deployed in different environments. I want to externalize some parameters, but want to provide default values for those parameters in case the deployed environment does not provide values for place holders.
This is what I have :
<property name="properties">
<props>
<prop key="embed.broker.networkConnectorURI">static:(failover:(tcp://server01:61616,tcp://server02:61616))
</prop>
<prop key="embed.broker.transportConnectorURI">vm://localhost:61616</prop>
</props>
</property>
<bean id="broker" class="org.apache.activemq.broker.BrokerService"
init-method="start" destroy-method="stop">
<property name="networkConnectorURIs">
<list>
<ref >${embed.broker.networkConnectorURI}</ref>
</list>
</property>
<property name="transportConnectorURIs">
<list>
<value>${embed.broker.transportConnectorURI}</value>
</list>
</property>
<property name="brokerName" value="embed-broker" />
</bean>
When I deploy this in an environment where the place holders are missing, Tomcat throws "Could not resolve placeholder 'embed.broker.networkConnectorURI' " error. In other words, the default values are not being picked up.
Any help would be appreciated.
To have some default values, go on this way:
<bean id="myServer" class="com.gordondickens.myapp.MyServerConfig">
<property name="serverName" value="${server.name?localhost}" />
<property name="serverPort" value="${server.port?25}" />
</bean>
Use a PropertyOverrideConfigurer instead of a PropertyPlaceholderConfigurer. That way your defaults specified in the context file will be used if no overriding property file entries are found.

How to set properly the loader path of velocity

i would like that my velocityengine look for templates from a designed path.
i did this :
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.resourceLoaderPath=/mytemplates
</value>
</property>
but is still looking for templates in the classes folder.
any idea?
As illustrated in the spring documentation, you could try the following:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">
org.apache.velocity.runtime.resource.loader.FileResourceLoader
</prop>
<prop key="file.resource.loader.path">${webapp.root}/WEB-INF/velocity</prop>
<prop key="file.resource.loader.cache">false</prop>
</props>
</property>
</bean>
Alternately, you could declare these properties in a velocity.properties and specify that
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="configLocation" value="/WEB-INF/velocity.properties"/>
</bean>
Try this:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/email_templates/"/>
</bean>
<bean name="mailTest" class="com.crisil.web.MailTestController">
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
Try with Resource Loader as
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Hope this helps.

Resources