Spring mvc:resources needs to map to the same filename? - java

I'm getting a 404 in Jetty if the mvc:resources tag in my web-context.xml is not mapped to the same filename:
<mvc:resources mapping="/some-file" location="/WEB-INF/js/some-file.js" />
Where as the following works fine:
<mvc:resources mapping="/some-file.js" location="/WEB-INF/js/some-file.js" />
How can I map files to a different file-name in the URL space?

I recommend you don't map single files but an entire directory.
<mvc:resources mapping="/js/**" location="/js/" />
As you can see the mapping attribute takes an ant path to match multiple files and directories.
For more information see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-static-resources

Related

Read PropertyPlaceholderConfigurer classpath Spring with Jboss

Using PropertyPlaceholderConfigurer to externalize spring-configuration
properties.
Added following code to spring-servlet.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:environment.properties</value>
</property>
</bean>
Filter to be externalized from spring-security.xml
<security:custom-filter position="AUTH_FILTER" ref="${filter}" />
filter value is present in environment.properties
environment.properties file is present inside Jboss modules and is readable from code using resource bundle.
But, with these changes somehow properties file is not getting loaded and following error is thrown while publishing code.
Caused by: java.lang.IllegalArgumentException: Could not resolve
placeholder 'filter' in string value "${filter}"
PS:
Also tried hardcoding path as <value>file:${jboss.home.dir}/modules/system/layers/base/configuration/main/environment.properties</value> but, dosen't seems to be working.
I think your problem is that your spring-servlet.xml is not linked to your spring-security.xml. So spring-security.xml has no knowledged of the PropertyPlaceholderConfigurer.
IMO, you should configure PropertyPlaceholderConfigurer in a properties-context.xml (for instance) so you can import this new file into your spring-servlet.xml and spring-security.xml as following:
<import resource="classpath:properties-context.xml" />

springmvc maven link files in jsp file

I use eclipse, springmvc and maven. I created a folder under
Web-Content --> WEB-INF --> resources
I added the following into my servlet:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />
Then I try to include a picture into my index.jsp file.(Web-Content->index.jsp)
<img src = "/resources/images/Logo.png" />
But it's not loading my image. I tryed many tutorials but it doesn't work. I guess I'm something basic wrong. Can you tell me whats wrong?
EDIT:
my serlvet got this
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
In my jsp file I did this:
<img src="<c:url value="/resources/logo.png" />"/>
I got resource folders with the logo.png file in the following places:
main->resources
main->webapp->resources
main->webapp->WEB-INF->resources
It still doesn't work
I also tried :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
and include the picture with
/resources/images/logo.png
and the picture located in
src/main/webapp/images/logo.png
doesnt't work for me
EDIT:
I guess it this:
<mvc:resources mapping="/resources/**" location="/resources/" />
got no effect to the program. I can always acces the url http://localhost:8080/test/resources/logo.png
doenst matter if I got this mvc line in my serlvet or not. If I change
<mvc:resources mapping="/resources/**" location="/resources/" />
to
<mvc:resources mapping="/resources1/**" location="/resources/" />
I can't acces to the file with http://localhost:8080/test/resources1/logo.png but with http://localhost:8080/test/resources/logo.png
Greets Sam
As an aside, while it is probably not best to make an Eclipse Dynamic Web Project and convert it to a Maven project through Eclipse, it will work, because Eclipse will put an entry into your POM that points to the WebContent directory. Have a look you will probably find it there. There is nothing wrong with making a Dynamic Web Project per se, but I typically change the initial directory configuration to match maven's expectations in the Wizard and convert to a maven project right away. Much easy to work on the POM than copy jars to the lib directory.
Otherwise, the resources directory you referring to should be in the root of your war file. The best way to get it there is to put resources at the root of your WebContent directory, not under WEB-INF. Try moving it there, open the .war file and inspect the contents to be sure it is in the root, and you should be good to go.

Icons not showing in Spring project with Bootstrap

I am utilising the Bootstrap front end framework, although I am having some trouble getting the icons to load. I have implemented Spring security, I am not sure whether this may be affecting them not to load. I am calling my .css and .js files like this:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/style/bootstrap.css" />
<script src="${pageContext.request.contextPath}/static/js/jquery.js"></script>
This is the configuration that I have in my Dispatcher servlet config:
<mvc:resources location="/resources/" mapping="/static/**" />
This is really irritating, can someone please help. Thanks
In your spring security config, have you excluded your resources directory containing your css, js, images from the secured zone ?
<http use-expressions="true" security="none" pattern="/resources/**">
</http>
If the problem is only related to the twitter bootstrap icons, have you included the files glyphicons-halflings.png glyphicons-halflingswhite.png in a resources/img folder ?

How to use property-placeholder for file on filesystem

We used to have a way to load properties from a file on the classpath:
<context:property-placeholder location="classpath:myConfigFile.properties" />
and it worked great. But now we want to load properties from a specific file on the system that is NOT in the classpath. We wanted to be able to dynamically load the file, so we are using a Java environment variable to populate it. I'll give a simple example below:
In Java:
System.setProperty("my.prop.file", "/path/to/myConfigFile.properties");
In Spring XML:
<context:property-placeholder location="${my.prop.file}" />
I've also tried it this way, thanks to an idea from Luciano:
<context:property-placeholder properties-ref="prop" />
<util:properties id="prop" location="reso"/>
<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="${my.prop.file}" />
</bean>
Everything I've tried has failed. No matter what I set my.prop.file to. Greatest hits include:
<context:property-placeholder location="/path/to/myConfigFile.properties" />
(ClassNotFoundException: .path.to.myConfigFile.properties)
<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
(ClassNotFoundException: file:.path.to.myConfigFile.properties)
<context:property-placeholder location="file:///path/to/myConfigFile.properties" />
(ClassNotFoundException: file:...path.to.myConfigFile.properties)
How do you use property-placeholders with a location that is on the file system and NOT on the classpath? We are using Spring 3.0.5.
It turns out there was a problem with the script running the Java program that loads the spring file. Thank you for helping. I am going to request that this question be deleted, as the original code works after all.
Thank you for your help.
This did work for me:
<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
But this (interestingly) did not:
<context:property-placeholder location="#{ systemProperties['foo'] }" />
I got
java.io.FileNotFoundException: Could not open ServletContext resource [/#{ systemProperties['foo'] }]
You're not going to be able to use a property placeholder ${..} with the definition of a PropertyPlaceholderConfigurer. It's the chicken before the egg.
You can always subclass PropertyPlaceholderConfigurer and have it do whatever you want (for example, call setLocations() in a #PostConstruct method. Then, instead of using <context:property-placeholder>, use:
<bean class="com.foo.MyPropertyPlaceholderConfigurer"/>
What about this way?
<context:property-placeholder properties-ref="prop" />
<util:properties id="prop" location="reso"/>
<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="/yourpathandfile" />
</bean>

finding a property using Spring and Webapp properties placeholder

This is a "simple" problem and I am seeking both a how-to and/or a you're-dumb-don't-do-that. I am open to both.
I am building a war file and want the structure to be:
WEB-INF/
properties/
<my properties files>
classes/
...
spring/
<my spring files>
Is that dumb? I know that I can access the properties files though the property-placeholder but I'd rather not nest the properties in the classes section - it doesn't make sense to me.
So the Spring file looks like this:
<context:property-placeholder location="classpath:properties/*.properties" />
if I want to access them in the classes area. I thought that
<context:property-placeholder location="properties/*.properties" />
would let me just put the directory under WEB-INF directly...am I wrong (ps I think I am :) ).
Any advice?
This should work
<context:property-placeholder location="WEB-INF/properties/*.properties" />
WEB-INF is not the root of the of the web-app, so you need to add WEB-INF to the path.
spring-context-3.1.xsd
<xsd:attribute name="location" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The location of the properties file to resolve placeholders against, as a Spring
resource location: a URL, a "classpath:" pseudo URL, or a relative file path.
Multiple locations may be specified, separated by commas. If neither location nor properties-ref is
specified, placeholders will be resolved against system properties.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
You can't do it the way you want since the classpath for the Classloader will be the /classes directory and any jars in the /lib directory. This is the standard configuration for a war file.
Wars and ears have specific configurations which you have to follow for the files to be valid. If you think about it, it would make it difficult to have different vendors provide web containers that could deploy the same war file if there was no standard format. There is a pretty informative page here.
To achieve something similar to what you want, you can simply have directories of /classes/properties and /classes/spring and look them up appropriately from your classpath ("classpath:properties/myfile.properties).
I am not sure what you want to achieve. Here the method I use to inject the properties from a basic properties file to a bean:
In the spring files (XML bean definitions), I add the reference to my property file (myfile.properties):
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myfile.properties" />
</bean>
and then I add my references to the properties (db.url is the URL address for my database connection, I kept only the bean properties referenced in my property file).
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.login}</value></property>
<property name="password"><value>${db.password}</value></property>
</bean>
By default, if the property is not defined in the property file, Spring uses the System Properties (this behaviour can be changed).

Categories