SPRINGMVC accessing static resources [GO]

Source: Internet
Author: User

1, if you only configure the interception of URLs similar to *.do format, then access to the static resources is not a problem, as follows:

<!--SPRINGMVC Core distributor--    <servlet>         <servlet-name>dispatcherServlet</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         < init-param>             <param-name>contextConfigLocation</param-name>             <param-value>classpath *:config/dispatcher-servlet.xml</param-value>         </init-param>         <load-on-startup>1</ load-on-startup>     </servlet>     <!--mapping *.do requests---    <servlet-mapping>         < Servlet-name>dispatcherservlet</servlet-name>         <url-pattern>*.do</url-pattern>     

2. If the configuration intercepts all requests, the following:

<!--SPRINGMVC Core distributor--    <servlet>         <servlet-name>dispatcherServlet</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         < init-param>             <param-name>contextConfigLocation</param-name>             <param-value>classpath *:config/dispatcher-servlet.xml</param-value>         </init-param>         <load-on-startup>1</ load-on-startup>     </servlet>     <!--map all requests--    <servlet-mapping>         < Servlet-name>dispatcherservlet</servlet-name>         <url-pattern>/</url-pattern>     

Such a configuration, will cause JS files, CSS files, image files and other static resources can not be accessed.

1) Take a picture as an example and store a picture of a.png in the/IMG directory under the Web project root directory.

2) Access this image in the JSP page

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

3) access to this JSP via controller

Package Cn.luxh.app.controller;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping (value= "/res") public class Staticresourceaccesscontroller {        @RequestMapping (value= "/staticres") public    String Accessres () {        SYSTEM.OUT.PRINTLN ("Access to static Resources");        return "Getstaticres";    }}

4) The results of the visit are as follows:

You can see that the picture cannot be accessed.

5) SPRINGMVC provides <mvc:resources/> tags to handle this problem, in the SPRINGMVC configuration file Dispatcher-servlet.xml do the following configuration:

        <!--static resource access        -<!--similar to:/img/** URL requested resources are found in the/img/directory        <mvc:resources location= "/img /"mapping="/img/** "/>

6) After configuration, re-access, OK.

3, for convenience, you can put JS files, CSS files, picture files in the resource directory of the respective sub-directory, such as/resource/js,/resource/css,/resource/img. Then do a unified configuration on it.

<!--static resource access--><mvc:resources location= "/resource/**" mapping= "/resource/**"/>

Reprint Address: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html

SPRINGMVC accessing static resources [GO]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.