Java, Spring hooking themes properties file to css file - java

this is how I intialize theme beans:
<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="resources.theme-" />
</bean>
<bean id="themeChangeInterceptor"
class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver"
class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
<ref bean="themeChangeInterceptor" />
</list>
</property>
</bean>
this is my folder stucture:
these are the inside of my 3 properties files:
css=themes/black.css
css=themes/blue.css
css=themes/default.css
I have also tried these:
css=classpath:themes/default.css
css=/themes/default.css
css=./themes/default.css
Should the css even be in WEB-INF/classes. I've tried moving it out and in etc, but never quite seemed to get it to work.
Here is how I put it in my JSP:
<link rel="stylesheet" href="<spring:theme code='css'/>" type="text/css" />
This is now my output looks like:
This is part of JSP:
<span style="float: left">
def
|
blk
|
blu
</span>
This is my black.css.
body {
background-color: #888;
color: white;
}
My problem is, that the css never seems to go on my JSP file. This is probably caused because properties file can not find css file...
Feel free to ask further info.

I think there is a problem with this.
<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="resources.theme-" />
</bean>
According to the Javadoc:
public void setBasenamePrefix(String basenamePrefix)
Set the prefix that gets applied to the ResourceBundle basenames, i.e.
the theme names. E.g.: basenamePrefix="test.", themeName="theme" ->
basename="test.theme".
Note that ResourceBundle names are effectively classpath locations: As
a consequence, the JDK's standard ResourceBundle treats dots as
package separators. This means that "test.theme" is effectively
equivalent to "test/theme", just like it is for programmatic
java.util.ResourceBundle usage.
Also, you're allowed to put the theme properties files inside the WEB-INF/classes folder as written in the Reference Docs.
By default, the ResourceBundleThemeSource uses an empty base name
prefix. As a result, the properties files are loaded from the root of
the classpath. Thus you would put the cool.properties theme definition
in a directory at the root of the classpath, for example, in
/WEB-INF/classes. The ResourceBundleThemeSource uses the standard Java
resource bundle loading mechanism, allowing for full
internationalization of themes.
I guess the problem in your case is that you've mentioned the path to the css twice.. Once in the <property name="basenamePrefix" value="resources.theme-" /> and once in the properties file too
css=themes/black.css
css=themes/blue.css
css=themes/default.css

What I needed to do here, was to move my resources(js or css files) to root folder of my project.
The second thing I could do instead, is to create a resource mapping for my css files.
Both would work, but I went with the first solution at the moment.

Related

Spring Data Context - lookup placeholder variables from Java map, not properties file

Currently in my data context XML file, it's looking up values to substitute from a application.properties file:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="default">
<property name="location" value="classpath:application.properties" />
</bean>
<bean id="appleDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${apple.url}" />
</bean>
I'd like to change this from being looked up from the application.properties file, to being read out of a Properties/Map object.
This is because the configuration could come from more than one place (i.e. not just the classpath), and this logic is already implemented in my application.
I've seen MapPropertySource but I think while that can be done when the bean is configured in Java, I'm not sure this logic can be implemented when working with the XML file alone?

Spring Message Not Displaying

I am trying to do internationalization in Spring-MVC for the first time and I'm having what I assume to be a configuration issue. I have a NLS file that I named NLS_en.properties which I placed in my application's WEB-INF\classes directory. The file contains the following NLS string:
MSG_HELLO = Hello to the Internationalized World
In my application's servlet.xml file I've defined the following beans:
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/NLS"/>
</bean>
In my JSP file I have the following tag:
<p><spring:message code="MSG_HELLO" text="You should not be seeing this text" />
When the JSP displays, of course, the message I see is the one I should not be seeing, so how do I have to configure my application so that I do see my HELLO message?
ResourceBundleMessageSource basename (as opposed to ReloadableResourceBundleMessageSource) refers by default to the classpath, so you should have it like :
<property name="basename" value="NLS" />
Now, depending on how you build, even if configuring correctly the message source, it may have been erased at the time you run the application.
Do not place resources directly into classes (or any target directory in general). If you use maven place it directly into resources. If you dont use any build framework put it in the root of the source directory.

css file is not working in Spring Theme module. properties files are loaded properly but css are not working

I configured my confugration file ie. dispatcher-servlet.xml file for Themes using following beans
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean>
I have added 3 properties
as theme-black.properties,theme-blue.properties,theme-default.properties under the source directory.
in each properties file I added key-value pair as following
style=style/blue.css
style=style/black.css
style=style/default.css
i put style folder under Web-Content .
problem is this ResourceBundleThemeSource loaded properties file successfully but could not able to load css file.
In JSP file I have added follwing code
<link rel="stylesheet" href="<spring:theme code='style'/>"
type="text/css" />
for changing
<span style="float: right;"> <a href="?theme=default">
default</a> | blue | black
</span>
Please help me out if any issue is there........ please please please > Thanks in Advance
I don't have experience with Spring's theme support, however I spot a little mistake in your URL.
First your URLs are relative. You should always have absolute URLs (starting with /).
Second, when using any URL, you should use <c:url> or <spring:url> so that correct context prefix is used:
<spring:url var="cssUrl">
<jsp:attribute name="value"><spring:theme code="style"/></jsp:attribute>
</spring:url>
It is as simple as specifying the location of your static content (in you application-config.xml), and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/resources/"/>
And next you edit the themes properties file with :
style=resources/style/blue.css
And put blue.css file into: webapp/resources/themes
Now you can load css file using:
"resources/themes/blue.css" in href
Or using style in code of spring:theme tag.
This works for me.
I don't know whether you have put the bean themeChangeInterceptor ref under bean DefaultAnnotationHandlerMapping . If you have done that , then please remove that from there and put that inside the <mvc:interceptors>tag like below
<mvc:interceptors>
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>

Spring - how to use PropertyPlaceholderConfigurer to dynamically load files

i have the next properties files with Spring Framework
config.properties
with content
environment=devel //posible values: devel, testing, prod
and with the previous environment property, choose some of the following files to load dynamically
config-service1-devel.properties
config-service1-testing.properties
config-service1-prod.properties
config-serviceN-devel.properties
config-serviceN-testing.properties
config-serviceN-prod.properties
and then, with spring i want load the properties, i'm solve to load the first properties file but i dont understand how to use expression language to complete the values of the dependent properties.
<bean id="MainApplicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="file://#{systemProperties['jboss.server.home.dir']}/conf/services.properties" />
<property name="placeholderPrefix" value="$mainProperty{" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="SecondApplicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="MainApplicationProperties">
<property name="locations">
<list>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceOne/service1-$mainProperty{environment}.properties</value>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceTwo/service2-$mainProperty{environment}.properties</value>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceN/serviceN-$mainProperty{environment}.properties</value>
</list>
</property>
</bean>
the error output is the next,
java.io.FileNotFoundException: /..../conf/serviceOne/service1-$mainProperty{environment}.properties (No such file or directory)
my opinion is, the value has not replaced
helpme, thanks
The problem is that when BeanFactoryPostProcessors are starting to be invoked, they are already instantiated. So even thou the first PropertyPlaceholderConfigurer modifies the bean definition of the second PropertyPlaceholderConfigurer, it has no effect as both beans have been already instantiated.

Spring MVC - Is it possible for a jar file to refer to a properties file that is not in the jar file?

I have a spring MVC web application that has the following structure:
myapp
|-META-INF
|-WEB-INF
|-classes
| |-conf
|-application.properties
|-lib
| |-externalApp.jar
| |-conf
| |-applicationContext.xml
|
|-applicationContext.xml
|-myapp-servlet.xml
In myapp/WEB-INF/applicationContext, i imported the applicationContext.xml file that is in the jar file as shown below:
<import resource="classpath:WEB-INF/conf/applicationContext.xml" />
The beans in the imported resource work fine and i can see them in my web application's controller/service classes.
The problem i have is that the
context file in the jar file (i.e. WEB-INF/lib/externalApp.jar/applicationContext.xml) has configuration for loading a properties file. The properties have to be set by the web application so the properties file is in the webapp. The configuration in the jar file's context file looks like this:
I want the above property to load the property file that is in the web application so i set its value to be as shown below:
<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
<property name="location" value="classpath:conf/application.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
With the above setting, the classes in the jar file that expect these properties to be availbe still cant get access to the properties.
The question i guess is how can i get the properties file that is in WEB-INF/classes/conf/application.properties to be accessible to the objects in the jar file that is located in WEB-INF/lib/externalApp.jar.
Looking at the stack traces i am getting, it looks as though the objects referred in the imported context file are loaded first before the properties are loaded which is not i want.
Thanks.
You can use the classpath*: prefix like this
<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
<property name="location" value="classpath*:conf/application.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
EDIT
Since your conf/application.properties is in your web app, you must define it in your web-app applicationContext (not in the jar as you do now). And define it before importing the applicationContext of your jar. i.e. put something like this in your web-app applicationContext:
<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
<property name="location" value="classpath:conf/application.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<import resource="classpath*:/conf/applicationContext.xml" />
and remove the declaration of the properties from your jar applicationContext.

Categories

Resources