How to set properly the loader path of velocity - spring

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.

Related

Configuring auto_escape in freemarker spring-mvc Application

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.)

how to set sessionFactory instance only once for entire application?

I am Injecting sessionFactory Through xml file(applicationContext.xml) using this package org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.
is there anyway to set the sessionFactory only once for entire web application? how can I achieve this ?
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mahesh</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
Unless you tell Spring otherwise, it will create each bean once per context (i.e. they will be singletons). Your XML will do just that, so it already does what you want.

Cannot get current session

`
classpath:database.properties
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="org.entity">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven/>
<!-- not working -->
<!-- <context:component-scan base-package="org.service"></context:component-scan> -->
`
this is my spring configuration file. Problem is that i am not getting current session in my repository classes which are marked with #Repository, I have annotated my service methods with #Transactional annotation.
However all the autowiring is working fine, the only problem is when i try to get current session in repository classes, and if I use bean tag to instantiate my repository and service beans it works. What am i doing wrong here?
Use the OpenSessionInView Filter or similar, that way you should have a Session available during the request lifecycle (Controller/ View rendering).

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>

Resources