Unable to retrieve properties with exposeContextBeansAsAttributes - spring

currently using spring 'exposedContextBeanNames' to allow me to display properties in my view but havign issues with some properties that contain a ".".
My xml is setup as
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list><value>classpath:servers.properties</value> </list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="exposedContextBeanNames">
<list>
<value>properties</value>
</list>
</property>
</bean>
My "servers.properties" has a number of values
value1=this is the first value
value.value1=this is my second value
In my JSP
${properties.value1}
will display "this is the first value" as expected but
${properties.value.value1}
does not work. I'm hoping someone may be able to help me. Thanks

Try ${ properties['value.value1'] }.

Related

spring bean optional property

I am using a data source defined in tomcat in my spring configuration as shown in the below xml.
It can happen sometimes that this data source may not be defined in the context.xml of tomcat.
In such cases , the context initialization fails since myDS is not found.
Is it possible to configure the datasource as optional so that application initialisation is not impacted ?
There can be a run time error when this data source is accessed , which is acceptable
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/myDS"/>
</bean>
<bean id="myEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="packagesToScan" value="com..XX.XX" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="#{systemProperties['showSql'] == null ? 'true' : systemProperties['showSql'] }" />
</bean>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<ref bean="wrkflw-punitpostprocessor" />
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">#{systemProperties['dbDialect']}</prop>
</props>
</property>
</bean>
Thanks
Muhad
You may check the DelegatingDataSource, you could encapsulate the logic to load the datasource from JNDI within its instantiation. For your application there will be always a DataSource there, but in some cases (whenever its not able to load the DataSource from JNDI) there is no delegation.

Spring multiple PropertyPlaceholderConfigurer (in multiple projects) how to override an already defined property?

I have a situation where I need to override properties from one project in another. In project 1, where I have no control over the source or the configuration, there is the following config:
<bean id="propSource1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/sample-properties/prop1.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="-10"/>
</bean>
In another project, which I am working on, I have following config:
<bean id="propSource2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/sample-properties/prop2.properties</value>
<value>classpath:/sample-properties/prop3.properties</value>
</list>
</property>
<property name="order" value="1000"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Individually both configurations work fine on their own. The problem happens when I have some property in prop2.properties or prop3.properties that I want to use to override the value for the same property from prop1.properties. It always uses the value in prop1.properties. I have researched quite a bit but did not find anything useful.
Any suggestion is appreciated.
The answer is in #M.Deninum's comment.

Access spring property in jsp or javascript

I have a pageSize variable configured in spring properties. I need to access this pageSize property in almost all the jsps. What is the best way to get hold of this spring property.
src\main\resources\web.properties contains default.page.items.size=10
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
I know how to access the property in the controller but as this property is been accessed my mutliple pages hence i want some way to access it javascript or jsp directly
I used this in a old project (not sure if still works) in your dispatcher context declare a bean like:
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
and then, if using standard JSTL view resolver:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="exposedContextBeanNames">
<list>
<value>myProps</value>
</list>
</property>
</bean>
You should be able to access the properties inside JSP using ${myProps.XXX}

Internationalisation with Spring

I have an existing Spring/GWT Application which i need to add internationalisation to. My understanding is that i can use Spring's "ResourceBundleMessageSource" to automatically select the appropriate messages_* file depending on the users location. I tried following this tutorial but i can't seem to get the Application to display my strings in French. As it stands, I've added 2 files messages_en_US.properties and messages_fr_FR.properties into my src/main/resources/i18n folder and added the following to the applicationContext.XML:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>classpath:i18n/messages</value>
</property>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Just wondering 1) if i need additional configuration/glue code and 2) if i can test this easily without having to set the Language/Locale to French on my Redhat Server?
It's likely that your browser sends just "fr" language tag in Accept-Header. Spring is notorious for problems with fall-back, so you may need to copy the messages_fr_FR.properties as messages_fr.properties.
I am sure there must be some ways to configure fall-back, so you want have to use messages_en.properties (try your application with other English locales...), just messages.properties should do, but I am just too lazy/tired to look for solution at the moment.
Here you need to specify below bean in spring.xml.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
This works perfectly fine when you pass lang=es in query string. If still any issue remain.you can check the working example Here .

Heritrix: how to exclude everything but pdf from mirroring?

I found this topic How do i exclude everything but text/html from a heritrix crawl?
I have changed bean to this
<property name="shouldProcessRule">
<bean class="org.archive.modules.deciderules.ContentTypeMatchesRegexDecideRule">
<property name="decision" value="ACCEPT" />
<property name="regex" value="^application/pdf.*"/>
</bean>
</property>
</bean>
But heritrix still saves every file to mirror dir.
I believe you are missing a reject rule above your accept rule. I have the following that works:
<property name="shouldProcessRule">
<bean class="org.archive.modules.deciderules.DecideRuleSequence">
<property name="rules">
<list>
<bean class="org.archive.modules.deciderules.RejectDecideRule">
</bean>
<bean class="org.archive.modules.deciderules.ContentTypeMatchesRegexDecideRule">
<property name="decision" value="ACCEPT" />
<property name="regex" value="^application/pdf.*"/>
</bean>
</list>
</property>
</bean>
</property>
This rejects everything, then accepts everything listed in the following rules.

Resources