2012-07-27 44 views
0

我的web.xml包含诸如图像,JS,CSS等存储在resoruces目录由相对路径如何在web.xml中正确映射Spring应用程序的内容?

<?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> 

的对其他静态资源资源/ index.html中引用。

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

但是,如果我在浏览器中放置了,则所有内容都正确显示。

所以,问题是我该如何让url中的欢迎页面成为<welcome-file>中给出的路径,例如: /resources/index.html。

如果无法在<welcome-file list>中完成,还应该使用其他可配置的方法。

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

+0

你应该添加更多的标签,如'spring','java'... – 2012-07-27 04:50:35

回答

1

看来你正在使用Spring并且遇到了静态内容的问题。

尝试寻找这个link

它说明了如何在这种情况下继续...

收费atention到行:

<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'...它更简单的映射。

+0

Vitor,泉源资源映射是定义的,但并不能解决问题。问题是''不会将完整路径resources/index.html复制到url。我有一种感觉,我必须使用重定向。 – YukunIX 2012-07-27 06:31:45

+0

按照Vitor的建议添加了spring configs,但它仍然没有得到.css和.js文件,尽管它显示了index.html。我认为我需要的是永久性的URL重定向到/resources/index.html。 – YukunIX 2012-08-02 04:11:57

相关问题