Spring4 Spring MVC实战(三)——Spring MVC不通过xml配置访问HMTL和其他静态资源

时间:2022-12-10 21:39:59
先看一下xml配置的,很多博客写出来都差不多,但是又不详细。
直接看一下老外的回答,How to handle static content in Spring MVC?


国内的博客里面一般就这样写。
<servlet>      <servlet-name>springMVC</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


就写出一块,其实我们可以写的再详细点,贴个完整的路径和配置出来。


WebContent/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>  <servlet-name>springmvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>  <servlet-name>springmvc</servlet-name>  <url-pattern>/</url-pattern> </servlet-mapping></web-app>


WebContent/WEB-INF/springmvc-servlet.xml:


<?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-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information --> <context:component-scan base-package="springmvc.web" />    <!-- the mvc resources tag does the magic --> <mvc:resources mapping="/resources/**" location="/resources/" />    <!-- also add the following beans to get rid of some exceptions --> <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> </bean>    <!-- JSTL resolver --> <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="viewClass"   value="org.springframework.web.servlet.view.JstlView" />  <property name="prefix" value="/WEB-INF/jsp/" />  <property name="suffix" value=".jsp" /> </bean></beans>



Spring MVC实战(一)——读《Spring in action》搭建最简单的MVC中继承AbstractAnnotationConfigDispatcherServletInitializer的类自动的配置了DispatcherServlet和
spring的应用上下文。所以我是没在web.xml中配置任何东西的。


但是我又不想在xml配置之后才能访问到静态资源和HTML,找来找去没有我想要的,那怎么办,还是看文档。http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable
思路就是找到要搜索的关键词,static resource。


看到了文档中的HTTP caching support for static resources



@Configuration@EnableWebMvcpublic class WebConfig extends WebMvcConfigurerAdapter {    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/resources/**")                .addResourceLocations("/public-resources/")                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());    }}And in XML:<mvc:resources mapping="/resources/**" location="/public-resources/">    <mvc:cache-control max-age="3600" cache-public="true"/></mvc:resources>


由于CacheControl是4.2版本之后才有的,我当前是4.1版本,所以去除setCacheControl方法。
现在的整个WebConfig.java



import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration@EnableWebMvc@ComponentScan("spittr.web")public class WebConfig extends WebMvcConfigurerAdapter {  @Bean  public ViewResolver viewResolver() {    InternalResourceViewResolver resolver = new InternalResourceViewResolver();    resolver.setPrefix("/WEB-INF/views/");    resolver.setSuffix(".jsp");    return resolver;  }    @Override  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {    configurer.enable();  }    @Override  public void addResourceHandlers(ResourceHandlerRegistry registry) {  registry.addResourceHandler("/static/**")      .addResourceLocations("/static/");  }}




将test.html 置于WebContent/WEB-INF/static目录
此后再访问http://localhost:8080/projectname/static/test.html即可访问,而无需在web.xml配置任何的东西。