I'm developing locally using both jetty and tomcat.
My images, css, javascript files are in:
/src/main/webapp/assets
where the folder assets has:
/src/main/webapp//assets/images
/src/main/webapp//assets/css
/src/main/webapp//assets/images/
My spring config file has:
<mvc:resources mapping="/assets/**" location="/" />
I'm confused as to what both mapping and location mean?
I think mapping means that that spring will only try and serve the static files if it has the url with the pattern like:
www.example.com/assets/
What does location do?
My html currently has:
src="/assets/images/logo.gif"
I've tried playing with the location value, and I don't get to render the image for some reason.
Can someone clear this up for me?
If your project structure has /src/main/webapp/assets/images, then you want to use:
<mvc:resources mapping="/assets/**" location="/assets/" />
and then in your JSP reference files as
src="${pageContext.request.contextPath}/assets/images/logo.gif"
Its more common to have a project structure like /src/main/webapp/images|css|js and then use:
<mvc:resources mapping="/assets/**" location="/" />
but still keeping URLs as ${pageContext.request.contextPath}/assets/images/logo.gif
Related
we had to configure the resource servlet of Spring in our app. It is a multi-module Maven project and consists of a WAR-file containing several jars with the Java app itself and UI modules with static content.
The static content (js and css) inside the jars is located under "META-INF/resources" and as well packaged like that. So from this point of view this is correct.
We introduced following in our coding (XML-based config):
<mvc:resources mapping="/resources/**" location="classpath:/META-INF/resources/,/" cache-period="2419200">
<mvc:resource-chain resource-cache="true">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>
It doesn´t work. The js and css files cannot be found in the classpath. I checked the local tomcat installation and the corresponding jars with the UI modules. These are located in WEB-INF/lib of the webapp´s directory.
If we copy the files into the webapp´s dir of tomcat they can be found and everything works correctly.
Any suggestions? Is this a possible bug in spring?
Thanks in advance!
I am trying to display an image on a jsp.
My image file is located at
MyApp/WebContent/images/logo.jpg
And my JSP pages are located at
MyApp/WebContent/WEB-INF/view/home.jsp
I have already tried to use the image by
<'img src="<%=request.getContextPath()%>/images/logo.jpg" />
and
<'img src="<'c:url value='<%=request.getContextPath()%>/images/logo.jpg'></c:url></img>
Is this issue something because of my location hierarchy where I have placed my image?
Really appreciate your help. Thank you.
UPDATE:
I've found the solution to my problem in:
http://www.tutorialspoint.com/spring/spring_static_pages_example.htm
I just have to use resource mapping in my servlet.xml.
I really appreciate all of your kind answers. :)
Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml.
Add the following entry to your MVC configuration. I assume that your static files in resources folder.
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
<img src="/resources/images/logo.jpg" />
To avoid to have to indicate explicitly the context path you can use jstl core and do it like that
<img src="<c:url value="/images/logo.jpg"/>"/>
You can also check this thread about spring ressource and path
Spring 3 MVC resources and tag <mvc:resources />
try
<img src="/MyApp/WebContent/images/logo.jpg" />
Even though it is a Spring MVC app, it should still deploy as a normal webapp. Check your deployment to make sure, and also use the browser to test loading.
To make it work I had to do
in spring config:
<mvc:resources mapping="/resources/**" location="/resources/" />
In JSP:
<spring:url value="/resources/images" var="images" />
<img src="${images}/back.png"/>
TRY THIS !
ALWAYS WORKS FINE !
Create your img folder at src/main/resources
Copy the picture inside this folder called "img"
Write inside
Use this picture inside
check the screenshots and enjoy !
I put images folder under WEB-INF directory, after did fully configuration in the spring-dispatcher-servlet.xml file, I used this img src:< img src="projectname/../images/logo.jpg" /> in my jsp page, images display finally.
in springmvc-servlet.xml you should add <mvc:resources location="/WEB-INF/images/" mapping="/images/**" /> and in jsp <img src="images/logo.jpg" /> and you should create a folder under web-inf which is named images and in the web.xml your servlet mapping shoul be like that <url-pattern>/</url-pattern>.
I am developing a shopping cart in Sprng MVC from where seller can upload products image with its description. I am able to upload images to webapp/resources/images folder. now I have to load all these images to dashboard page (home page) when any user open my site. I am not able to load these images from this location.
In my jsp I am writing final code like this:
<img src="resources/images/product1.jpg"/>
how can I add this folder to classpath so it'll be available. I am using Spring MVC and MAVEN.
Please let me know how to achieve this.
With Expression Language (EL) in JSP:
<img src="${pageContext.request.contextPath}/resources/images/product1.jpg"/>
As I understand, you have uploaded your images to a separate folder on your server and you want to include the folder in the classpath to retrieve and show the images on your JSP.
In Spring MVC, you can force the use of the absolute path (relative to the root of the system) by prefixing 'file:' with the URL resource:
// actual context type doesn't matter, the Resource will always be UrlResource`
ctx.getResource("file:/root/webapp/resources/images");
or
// force this FileSystemXmlApplicationContext to load its definition via a UrlResource`
ApplicationContext ctx =
new FileSystemXmlApplicationContext("file:/root/webapp/resources/images");`
This is one way of adding your image directory by specifying the absolute URL.
If you have your image directory relative to your current directory and want to add to your class path, then the following ways will work:
ApplicationContext ctx =
new FileSystemXmlApplicationContext("resources/images");
This works relative to the current working directory.The images will be loaded from a filesystem location, in this case relative to the current working directory. so, we have added the directory to classpath. In the same way, you can make the classpath configuration in your XML as well.
<mvc:resources mapping="/resources/**"
location="classpath:resources/images"
cache-period="10000" />
or
<mvc:resources mapping="/resources/**"
location="file:/root/webapp/resources/images"
cache-period="10000" />
we will be able to retrieve the images now in your JSP as follows
<img src="${pageContext.request.contextPath}/resources/images/user.jpg"/>
The above EL ${pageContext.request.contextPath} ensures that the Context is always prepended.
Note: In Spring 3.2.2, the Context path is prepended automatically if not present.
I am able to fix this problem:
Added the following resources declaration to Spring configuration
<resources mapping="/resources/**" location="/resources/" />
and in jsp
<img src="<c:url value="/resources/images/product1.jpg" />" alt="" />
Thanks guys for your all help, your direction really helped me to get the answer for this problem.
I have a Spring Web MVC application that I'd like to serve a large, partially generated file.
I've added that file to my WebContent directory and all works fine there. However, I'd also like to access that file from my various build/deploy scripts, which read and parse the file.
My current approach is to keep a copy of the file under the src directory as well as the WebContent directory. When serving the file from the web, it uses WebContent.
When serving the file for the build scripts, it uses the following spring config:
<bean id="ringCodeData" class="com.myapp.data.RingCodeData">
<property name="rulesInputFile" value="classpath:resources/rules_copy.xml" />
<!-- <property name="rulesInputFile" value="classpath:../WebContent/rules.xml" /> -->
<!-- <property name="rulesInputFile" value="file:/WebContent/rules.xml" /> -->
</bean>
As you can see, I've tried several different approaches to get the two to refer to the same file (without resorting to copies).
File paths don't seem to work since they're based on the current directory, that changes based on whether I call a given utility class from Eclipse or from the build scripts.
How can I get these to refer to the same file?
The only other thought I have at the moment is to try to setup Spring MVC to stream the file from the classpath directory.
Your best bet is likely placing it in /WEB-INF/classes (or, if you're using an IDE, just the project's src/source folder) and use <jsp:include> to include it.
<jsp:include page="/WEB-INF/classes/resources/rules_copy.xml" />
I have seen several questions broaching this issue, but I haven't come across a clear answer.
I need to package a SQLite .db file into a WAR and work with that DB file within the WAR or exploded WAR.
My project is Spring, using Spring JDBC.
Everything works fine with the .db file in the classpath for testing of the non-web portion of the code. I can run unit tests against the database without problem using just
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="connectionInitSqls">
<list>
<value>PRAGMA foreign_keys = ON</value>
</list>
</property>
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:app2.db" />
</bean>
When I go to deploy, I'm able to pull the db into the WAR file only if I place it in the WebContent/resources directory. I can't seem to get this working with alternate URIs. (for example, jdbc:sqlite::resource:app2.db, jdbc:sqlite:WebContent:resources:app2.db, jdbc:sqlite:/resources/app2.db, etc.). I've seen answers suggesting that I tie it to tomcat's deployment directory location specifically, but I'd really rather not have the location be dependent on anything outside of the WAR.
Does anyone have a working example (using XML config for the datasource in Spring applicationContext) of a datasource mapping (includig jdbc URI string), and corresponding pom.xml maven-war-plugin specification (if applicable) to achieve the solution of letting the project work with a SQLite .db file stored with the project in its WAR?
Thanks in advance.
--EDIT (additional clarifying information in response to comments) --
I am able to get the .db into the classes subidirectory of the WAR via this directory (or through something in the WebApps subdirectory by a workaround resource mapping in the maven-war-plugin specification), but this is not the main problem.
My issue is: How do I reference the .db via relative URL/URI once it is within the WAR.
I need to point the jdbc driver to this location via a relative path so that it can be accessed regardless of deployment location.
I have options of absolute URL on the filesystem (I've gotten direct location on my computer to work with
or path within Tomcat, but again, by hard-coded URL, or best case, URL tied to CATALINA_BASE with
<property name="url" value="jdbc:sqlite:/var/lib/tomcat7/webapps/APPNAME/WEB-INF/classes/app2db.db" />
I've seen it suggested that I use
<property name="url" value="jdbc:sqlite:${catalina.base}/webapps/APPNAME/WEB-INF/classes/app2db.db" />
though this does not seem to work with my installation (and is still not an optimal solution, as it is tied to the surrounding deployment location).
jdbc:sqlite allows mapping of relative paths using no preceding forward slash, but I can't get this to work within the war with something like
<property name="url" value="jdbc:sqlite:WEB-INF/classes/app2db.db" />
This is what I'm looking for -- the relative path mapping such that I can send the .db off with the WAR and just tell someone to deploy to tomcat -- or any server, within reason, since I have the jars needed to execute mostly packaged up with the app through Maven --(even if their directory structure is unusual or they don't have path variables mapped), and have it still be able to access the database with rw access from within the exploded WAR.
When I go to deploy, I'm able to pull the db into the WAR file only if I place it in the WebContent/resources directory.
If I place my.db in Maven's default for resources <project>/src/main/resources/my.db it is packaged in /my.war/WEB-INF/classes/my.dbhere.