SpringMVC Static resource access

Let's talk about access to static resources

First , We need to web.xml Inside DispatcherServlet Set it up

<!-- Central scheduler -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--tomcat Once started, the central scheduler can be loaded , As long as the number is greater than 0 that will do -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--
url-pattern Set to :"/", When you use "/", He'll take the place Tomcat Medium default,
Cause all the resources to DispatcherServlet Handle , By default DispatcherServlet
No ability to handle static resources , No controller object can handle access to static resources , So it leads to static resources
Access failures are 404, The dynamic proxy can access it normally . If not set url-pattern Set to :"/",
There is no static resource that cannot be accessed
-->
<url-pattern>/</url-pattern>
</servlet-mapping>

Due to the url-pattern by :"/", Next we need to set up springmvc Configuration file for , Make static resources accessible to . There are two ways

  1. The first way
<!-- Add the tag -->
<mvc:default-servlet-handler />
<!--
principle :
Add a processor object to the program memory :DefaultServletHttpRquestHandler.
Let the object forward the received request to Tomcat Of default This servlet.
But this label is related to @RequestMapping There is a conflict in this annotation , So we need to add annotation driven :<mvc:annotation-driven />
-->
<mvc:annotation-driven />
  1. The second way
<!--
Use... In the frame :<mvc:resource mapping="" location=""/> label ,
After the tag is added, the framework will create itself ResourceHttpRequestHandler This processor object .
Let this object handle access to static resources , Don't depend on Tomcat The server . The label is associated with @RequestMapping
There are conflicting annotations , Need to annotate drive :<mvc:annotation-driven />
Parameters :
location: The location of static resources in your project
mapping: Access to static resources URI Address , Use wildcards ** , It stands for any
All static resources in parameters can be accessed
-->
<mvc:resources location="/img/" mapping="/img/**"/>

All right. , That's what it's like to be a rookie right now springmvc Mastery of static resource access . Welcome to leave a message

SpringMVC Next, more related articles about static resource access

  1. SpringMVC REST Style static resource access configuration

    1 stay web.xml Use the default servlet Processing static resources , The disadvantage is that if there are too many static resources , The amount of configuration will be relatively large , Once there's an omission , This will cause the resources not to be displayed or 404 error . <!-- Static resource access control --> ...

  2. springmvc、springboot Static resource access configuration

    How to access static resources in a project ? One .springmvc springmvc Access static resources in , If DispatcherServlet The interceptor is "", Then static resource access will also be given to Dispatch ...

  3. 【SpringMVC】 Static resource access issues

    Some static resources are often used in projects , And generally we're configuring SpringMVC Time will let SpringMVC Take over all requests ( Including access to static resources ), So how can we most simply configure static resource access ? One , stay web.xm ...

  4. About WEB-INF Static resources in the directory (js、css、img) The interview of

    First , Just to be clear WEB-INF The directory is protected , Below the jsp The page doesn't run directly , Can only be accessed by controller jump : And the same static resources in this directory (js.css.img) Also can't be WEB-INF Other files outside the directory are directly ...

  5. springMVC Configure static resource access &lt;mvc:resources&gt; Use of labels

    stay springmvc in , Access to resources for reference will not be similar to Controller I've been intercepted as well , Distinguish access to resources of interest , Usually we are in springMVC All interceptions inside will be configured as "/", Intercept all . But this ...

  6. springmvc The static resource of cannot be accessed - Remember a painful experience

    springmvc The static resource of cannot be accessed - Remember a painful experience Problem description : Normal project start , You can visit the page , But the static resource file cannot be found , Such as css,js And so on . Console : $ Undefined page : GET http:/ ...

  7. About linux Deploy JavaWeb project ,nginx Responsible for static resource access ,tomcat Responsible for processing dynamic requests nginx To configure

    1. The running environment of the project linux edition [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...

  8. spring mvc The latest jar Building framework - Static resource access processing - annotation - Automatic scanning

    1. Download from official website spring relevant jar http://spring.io/projects Click on SPRING FRAMEWORK

  9. Spring Boot Static resource access principle analysis

    One . Preface springboot There are many ways to configure static resources , Next, I'll introduce some of them , And analyze the principle . Two . Use properties Property configuration It should be said  spring.mvc.static-pa ...

  10. SpringMVC Static resources cannot be introduced

    SpringMVC Static resources cannot be introduced problem : After the project is deployed, a similar warning appears when the program is loaded or accessed with a browser , 2011-01-19 10:52:51,646 WARN [org.springframework.web ...

Random recommendation

  1. Talking about Extjs radiogroup change Incident and items Under the checked attribute

    In the use of Extjs Make crud when , Due to the high similarity between the add and modify interfaces , Used the same row Field . In the role field change Monitoring events , Field for dynamic invalid permission assignment , Because the permission allocation interface is not hidden by default , Set the che ...

  2. Java keytool Use summary

    Keytool It's a Java Data certificate management tools ,Keytool Key (key) And certificates (certificates) There is one called keystore In the file of . stay keystore in , There are two kinds of data : ( ...

  3. C# The image processing ( Compress 、 Tailoring , transformation , Optimize )

    1. Image to binary conversion Image conversion to binary is mainly used for database storage and image data transmission between applications , If the website server passes the WebService or WCF Get image data from data processing server , It can be transmitted in binary form /// Picture to binary ...

  4. ASP.NET The second error handling mechanism of ( example log4net)

    One .log4net download :http://logging.apache.org/log4net/download_log4net.cgi Two .web.config The configuration is as follows : <?xml versio ...

  5. Replace centos Source

    I'm installing... In my virtual machine GoAccess When , Say we can't find it GeoIP... embarrassed . I won't say GeoIP Things about , I want to update my yum Source , Because my system (Centos 6.5) Using the default source . Relatively slow , Here first ...

  6. A Byte of Python note (12)python Standard library :sys、os, More

    The first 14 Chapter python Standard library Python Standard library is attached Python Installed with , It contains a lot of extremely useful modules . sys modular sys The module contains the corresponding functions of the system . Such as sys.argv The list contains command line arguments . # ...

  7. note 《JavaScript Authoritative guide 》( The first 6 edition ) A summary of knowledge points 2— type 、 value 、 Variable

    [ type . value . Variable ] Numbers . Text . Boolean value .null and undefined. Global object . Packaging object . Immutable raw values and mutable object references . Type conversion . Variable declarations . Variable scope P44~71(PDF edition ).P32~59( Book edition ) ...

  8. Line segment tree HDU-1166 The enemy troops were arrayed

    The formation of enemy troops is a classic problem of line tree , The title is as follows ( Click here to see the source of the title ): Problem Description C The enemy of our country A China is conducting military exercises during this period , therefore C The head of the country Derek And his men Tidy I'm busy again .A countries ...

  9. spark Read multiple files using regular expressions

    String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...

  10. Solution run wamp Tips “MSVCR110.dll” Lost problems !

    I am here Windows Installation on system wampserver2.5 64 position , Install to the end , Always prompt for missing msvcr110.dll terms of settlement : Go to this website and download one Visual C++ Redistributable f ...