JBOSS_HOME spring resource mapping - spring

Hi guys I can't properly map my JBOSS_HOME location for example I have something like this:
<mvc:resources order="-10" location="file:/C:/Serwery/jboss-as-7.1.1.Final/" mapping="/test/**" />
But this is absolute path I want to make something like this:
<mvc:resources order="-10" location="file:/$JBOSS_HOME/" mapping="/test/**" />
Any advice ?

Just add this
<context:property-placeholder />
to your xml configuration where you have the <mvc:resources /> and use it like this (notice how the env variable is handled - ${JBOSS_HOME}):
<mvc:resources order="-10" location="file:/${JBOSS_HOME}/" mapping="/test/**" />

Related

Serving static content with Spring

I've been following a couple of tutorials on serving static files (CSS, JS etc.) using Spring and from what I can see I should be able to see my static files but I can't.
My spring-web-servlet.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="brass.ducks" />
<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" />
</bean>
<mvc:resources mapping="/styles/**" location="views" />
<mvc:annotation-driven/>
</beans>
The last line suggests that any URL that follows styles/whatever.css should look in my views folder and serve from there. For example my page has a link to a stylesheet as /styles/css/bootstrap.min.css (which would complete to http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css) but this does not resolve to my style sheet.
My folder hierarchy looks like this:
Am I missing something?
EDIT
As suggested I extracted my .war file and found the folder hierarchy looks like this:
I've modified my web.xml to look like this:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/" />
<mvc:annotation-driven/>
But the URL http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css still does not resolve a CSS file.
Move your styles directory to /webapp/resourses path. So you will have /webapp/resourses/styles. Change xml config with <mvc:resources mapping="/styles/**" location="/resources/styles/" and try to get from http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css
Change your mapping config to:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/styles/" />
Yes, you are missing the /, it should look like this:
<mvc:resources mapping="/styles/**" location="/views/" />
and XML is not really a good idea to configure spring, try java config..

spring mvc touch file and serve it as resource

How can I create a file and serve it as a resource, my /src/main/webapp/WEB-INF/spring/web/dispatcherServlet-context.xml file snippet:
<mvc:resources mapping="/resources/**" location="/resources/" />
Any nice approaches?

Resource : Single location to multiple mappings

When I use in my application:
<resources location="/resources/favicon.ico" mapping="/favicon.ico" />
to map out single resource.
How can I map single resource to be accessible through multiple mappings without need to duplicate the file locally ?
For example like this:
<resources location="/resources/favicon.ico" mapping="/favicon.ico" />
<resources location="/resources/favicon.ico" mapping="/favicon.png" />
You can include wildcards. mapping="/favicon.*"

Can you configure velocity to use a toolbox file from the classpath?

I'm using spring with a VelocityViewResolver configured by Spring
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>
This works if the velocityToolbox.xml file is under WEB-INF ; however I'm trying to put it in the classpath. Is there a way to do it ?
I've tried specifying toolboxConfigLocation as "classpath:/wherever/velocityToolbox.xml", but it does not find the resource, and I end up with no toolbox configured, and a NPE at runtime (for some reason, it seems like the code expect the location to start with a '/', or add the '/' itself before looking for the resource).
Given that the resource is located using ServletContext.getResourceAsStream, with the content of the toolboxConfigLocation property prefixed by a "/", is there a way I can "define" a resource in my spring config that would somehow 'point' to an actuall classpath resource ?
Any idea is welcome.
I believe it has to be a full path, eg:
/WEB-INF/classes/com/myapp/resources/toolbox.xml
The ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) method is used to generate a ToolboxManager configured with the toolbox in order to create the ChainedContext used in the VelocityToolboxView.
This method pre-appends the path with a '/' if one does not exist, and then uses ServletContext#getResourceAsStream(String path) to read it in.
With this in mind, you'll have success if you set it as a full path from the context root.

Load jndi resources into context:property-placeholder

I have the following spring config file:
<context:property-placeholder order="2"
ignore-unresolvable="true" ignore-resource-not-found="true"
location="file:///${user.home}/application.properties" />
<context:property-placeholder order="1"
ignore-unresolvable="true" ignore-resource-not-found="true"
location="file:///C:/Services/Tomcat 6.0/cms/application.properties" />
<context:property-placeholder order="3"
location="classpath:com/afrozaar/cms/service/application.properties" />
Notice how they are ordered, some are on the classpath and some are on the file system.
Now to the mix I want to add a properties file loaded via jndi. I was hoping to be able to do
<context:property-placeholder order="2"
ignore-unresolvable="true" ignore-resource-not-found="true"
location="jndi:url/application.properties" />
Unfortunately, this doesn't work, spring doesn't support the jndi prefix... AFAIK.
So, can I do something like this?
And if I can't what's my alternative. I don't want to have to convert my whole configuration to a full bean based property place holder configurer.
Not sure what you really mean with "jndi:url/application.properties". I suppose you wanted to set the path to the property file in a resource entry named "url/application.properties".
You can achieve this with the following snippets:
<bean class="org.springframework.beans.factory.config.PlaceholderConfigurerSupport">
<property name="location">
<bean id="publisherLocal" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="url/application.properties" />
<property name="expectedType" value="java.lang.String" />
</bean>
</property>
</bean>
<context:property-placeholder> has a properties-ref attribute, which can point to the bean of type Properties. So, you can load Properties in your code and declare a <context:property-placeholder> using them.

Resources