springspring-mvcxml-configuration

As i ahve not kept any file in resource folder why we need to map to resource folder to load all static Resource?


its contains a tag mvc:resource tagI am developing a spring mvc application in which iam using xml based configuration.I want to load my static resource.To do this i am using a tag <mvc:resources location="/assets/" mapping="/resources/**"/>.

All my static resources is present in assets folder so i am giving the loaction. but in mapping why it is mapping="/resources/**"? I have not kept anything in resource folder and what does ** represents?

Can anyone clear all my doubts here?


Solution

  • <mvc:resources location="/assets/" mapping="/assets/**"/>
    

    Update mapping="/assets/**", It will work fine. In Mapping attribute you should keep the matching url pattern to access the assets folder.

    ** :- Means multiple directories in this path or Zero directories after assets directory.

    Take an example:-

    <script type="text/javascript" src="${contextRoot}/assets/js/login.js"></script>
    

    You want to add a java script file in your page. To add this js file, the server request become : http://localhost:8090/Aashayein/assets/js/login.js . As you are using spring-mvc so the request goes through dispatcher servlet(front controller). For this request Uri there is no corresponding handler marked (@RequestMapping("/assets/js/login.js")), so it results 404 error. So you have to tell spring that if any request comes with uri pattern "/assets/**" then directly looks to the assets folder present in the webapp directory. To do so in xml we have to write:-

    <mvc:resources location="/assets/" mapping="/assets/**"/>
    

    In annotation:-

    /*
     * Any request with url mapping /assets/** will directly look for /assets
     * folder.
    */
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    
       registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
    }