Announcement Announcement Module
Collapse
No announcement yet.
<mvc:resources> or how can i get and use the file from my application? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • <mvc:resources> or how can i get and use the file from my application?

    Hello all!
    I need to use some file from my application in it, how can i do this?
    I tried to do this with <mvc:resources> but it didn't work... Maybe i doing smth wrong, please help me.

    My needed file is located in the src\main\webapp\META-INF\resources\file.xsl

    In the code i try to get this file by:
    Code:
    ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:/WEB-INF/spring/root-context.xml");
    Resource resource = appContext.getResource("classpath:/META-INF/resources/file.xsl");
    But on the log:
    Code:
    java.io.FileNotFoundException: class path resource [META-INF/resources/file.xsl] cannot be opened because it does not exist
    	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
    getFileFromDb-servlet.xml
    Code:
    <?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-3.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
     		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <context:annotation-config />
    	
        <mvc:annotation-driven/>
        
        <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
     	<mvc:resources location="/, classpath:/META-INF/resources/" mapping="/resources/**"/>
     	<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
     	<mvc:default-servlet-handler/>
    </beans>
    root-context.xml
    Code:
    ...
    <import resource="classpath:WEB-INF/getFileFromDb-servlet.xml" />
    ...
    web.xml
    Code:
    <servlet>
            <servlet-name>getFileFromDb</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>getFileFromDb</servlet-name>
            <url-pattern>/getFileFromDb/*</url-pattern>
        </servlet-mapping>
    .classpath contains this:
    Code:
    <classpath>
    	<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
    	<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
    	<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
    	<classpathentry excluding="**/*.java" kind="src" path="src/main/webapp/WEB-INF/resources"/>
    	<classpathentry excluding="**/*.java" kind="src" path="src/main/webapp"/>
    	...

    Thank you very much!

  • #2
    And why on earth do you expect mvc:resource to help you getting the file by constructing an ApplicationContext (which you shouldn't do unless you want to cripple your application at runtime)...
    The META-INF you mention (the on in src/main/webapp) isn't on the class path so it will never resolve... Use META-INF in src/main/resource instead...

    Comment


    • #3
      hi voolyaka,
      You are accessing a file located in META-INF and you are specifying "classpath:" as prefix which start looking from location classpath , so its obvious file wont be accessible

      Comment


      • #4
        yes, you are right, thank you very much! I removed <mvc:resource> from my config files and relocated file.xsl to the folder src\main\resources. Then i successfully get this file by Resource resource = appContext.getResource("classpath:/file.xsl");
        Thank you very much!

        Comment


        • #5
          You shouldn't do that... You shouldn't create a new application context each time you need the file... Why not simply inject the file instead of (re)loading it over and over....

          Comment

          Working...
          X