admin管理员组文章数量:1794759
Spring4 Spring MVC实战(三)——Spring MVC不通过xml配置访问HMTL和其他静态资源
先看一下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="java.sun/xml/ns/j2ee" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/j2ee java.sun/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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-2.5.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd"> <!-- not strictly necessary for this example, but still useful, see static.springsource/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" /> <bean class="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,找来找去没有我想要的,那怎么办,还是看文档。docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable 思路就是找到要搜索的关键词,static resource。 看到了文档中的HTTP caching support for static resources
@Configuration @EnableWebMvc public 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目录 此后再访问localhost:8080/projectname/static/test.html即可访问,而无需在web.xml配置任何的东西。
版权声明:本文标题:Spring4 Spring MVC实战(三)——Spring MVC不通过xml配置访问HMTL和其他静态资源 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686616902a86678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论