【问题标题】:How to map content properly in web.xml for a Spring application?如何在 web.xml 中为 Spring 应用程序正确映射内容?
【发布时间】:2012-07-27 04:19:03
【问题描述】:

我的 web.xml 包含

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      resources/index.html
    </welcome-file>
  </welcome-file-list>
</web-app>

resources/index.html 通过相对路径引用存储在 resoruces 目录中的其他静态资源,如图片、js、css 等。

当我在浏览器中输入http://localhost/MyProject/ 时,它显示了 index.html 但没有得到 css 和 javascripts。

但是,如果我在浏览器中输入http://localhost/MyProject/resources/index.html,一切都会正确显示。

所以,问题是我怎样才能让 URL 中的欢迎页面作为&lt;welcome-file&gt; 中给出的路径,例如/resources/index.html.

如果不能在&lt;welcome-file list&gt; 中完成,我应该使用什么其他可配置的方法。

我倾向于不通过添加另一个 html 或通过在 Servlet 控制器中以编程方式来重定向到 /resources/index.html。

【问题讨论】:

  • 你应该添加更多标签,比如'spring'、'java'...

标签: java spring web.xml


【解决方案1】:

您似乎在使用 Spring 并且遇到静态内容问题。

试试看这个link

它解释了在这种情况下如何进行......

注意线路:

<mvc:resources mapping="/resources/**" location="/resources/" />

它将您的资源文件夹(包含 css、javascript 和图像文件)映射到 Spring 的特殊处理程序。

更新:

在您的 servlet-context.xml 文件中,您可以添加这一行

<!-- Forwards requests to the "/" resource to the "welcome" view -->
    <mvc:view-controller path="/" view-name="index"/>

<!-- Resolves view names to protected .html resources within the /WEB-INF/resources/ directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/resources/"/>
        <property name="suffix" value=".html"/>
    </bean>

这表示您不必正确使用“index.jsp”。这样,您会将视图映射到“/”访问。总结一下,这样用户输入'http://localhost/MyProject/',看到你的index.html,看到css和javascripts的效果。

PS.: - 此配置仅适用于 Spring 3+
- 更喜欢将文件命名为“.jsp”而不是“.html”...映射更简单。

【讨论】:

  • Vitor,定义了spring资源映射,但并没有解决问题。问题是&lt;welcome-file&gt; 没有将完整路径资源/index.html 复制到 url。我觉得我必须使用重定向。
  • 按照 Vitor 的建议添加了 spring 配置,但它仍然没有获取 .css 和 .js 文件,尽管它显示了 index.html。我想我需要的是一个永久的 url 重定向到 /resources/index.html。
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 2012-07-23
  • 2017-07-22
相关资源
最近更新 更多