spring-mvc - Spring MVC 中如何处理静态内容?

标签 spring-mvc

我正在使用 Spring MVC 3 开发一个 Web 应用程序,并让 DispatcherServlet 捕获对“/”的所有请求,如下所示 (web.xml):

  <servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

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

现在这正如广告中所宣传的那样,但是我如何处理静态内容?以前,在使用 RESTful URL 之前,我会捕获所有 *.html,并将其发送到 DispatcherServlet,但现在情况不同了。

我有一个/static/文件夹,其中包含/styles/、/js/、/images/等,我想从 DispatcherServlet 中排除/static/*。

现在,当我这样做时,我可以让静态资源正常工作:

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

但我希望它有漂亮的 URL(我使用 Spring MVC 3 的目的)而不是登陆页面是 www.domain.com/app/

我也不想要一个与 tomcat 或任何其他 servlet 容器耦合的解决方案,并且因为这是(相对)低流量,所以我不需要前面的网络服务器(如 apache httpd)。

有没有一个干净的解决方案?

最佳答案

由于我在这个问题上花了很多时间,所以我想分享一下我的解决方案。从spring 3.0.4开始,有一个配置参数,叫做<mvc:resources/> (有关 reference documentation website 的更多信息)可用于提供静态资源,同时仍在站点根目录上使用 DispatchServlet。

为了使用它,请使用如下所示的目录结构:

src/
 springmvc/
  web/
   MyController.java
WebContent/
  resources/
   img/
    image.jpg
  WEB-INF/
    jsp/
      index.jsp
    web.xml
    springmvc-servlet.xml

文件的内容应如下所示:

src/springmvc/web/HelloWorldController.java:

package springmvc.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {
 
 @RequestMapping(value="/")
 public String index() {
  return "index";
 }
}

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" />
 <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>

WebContent/jsp/index.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Page with image</h1>
<!-- use c:url to get the correct absolute path -->
<img src="<c:url value="/resources/img/image.jpg" />" />

关于spring-mvc - Spring MVC 中如何处理静态内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1483063/

相关文章:

java - spring3.0 mvc问题(请求的资源不可用)

java - Spring 3.1 Web 应用程序问题

java - SpringSource Spring MVC 模板创建时出错

java - Spring MVC : JSON to Pojo conversion throwing HTTP 415 Error

javascript - AngularJS 从 Spring REST API 读取 JSON

java - HttpServletRequest 对象的字段会延迟截断。为什么?

java - Spring boot中 Autowiring 接口(interface)错误

java - 带有 { } 大括号的 Spring MVC @Path 变量

java - 对现有 webapp 进行 Spring Boot 健康检查

java - SpringMVC与静态资源