【问题标题】:Cannot access web application resources in Tomcat 7无法访问 Tomcat 7 中的 Web 应用程序资源
【发布时间】:2012-09-28 22:21:06
【问题描述】:

我正在使用 SpringMVC 和 JavascriptMVC 制作一个示例 Web 应用程序,部署了 Tomcat 7。我在我的应用程序中加入了 Spring 安全性和 Spring MVC,我从 Mkyong 的这篇文章中获得了帮助 http://www.mkyong.com/spring-security/spring-security-form-login-using-database/ 现在我的应用程序在我将其部署到 tomcat 时运行良好,但现在我想在我的应用程序中添加客户端 MVC,即 JavascriptMVC,我想在我的应用程序中添加脚本资源,如 js 文件。

这是我的应用程序的流程,用户使用启动应用程序

"//localhost:8080/SpringMVC(应用名称)/welcome(/welcome被控制器重定向到hello.jsp页面)"

这显示登录页面,一旦写入用户凭据,他就会进入 hello.jsp 页面。在这个页面中,我添加了我的 JacascriptMVC 代码并在标签中引用了一个 js 文件,该应用程序在我的浏览器本地运行良好,但是当我将它部署到 tomcat 上时,它说资源不可访问 404 错误并且只显示基本的 html,并且不能访问js和css文件。

我尝试了很多方法来访问资源,比如

<script src='./WebContent/javascriptmvc/steal/steal.production.js'/>

<script src='WebContent/javascriptmvc/steal/steal.production.js'/>

<script src='/WebContent/javascriptmvc/steal/steal.production.js'/>
但都给出了同样的错误。

我的war文件的应用目录结构

-SpringMVC
  -WEB-INF
    -pages
    -hello.jsp
    -login.jsp 
  -META-INF
  -WebContent
    -javscriptmvc
      -steal
         -steal.production.js

任何帮助将不胜感激。 法赫德

【问题讨论】:

  • AFAIk,您无需在路径中指定 WebContent 文件夹。将其视为这些资源的根文件夹。

标签: javascript spring-mvc spring-security tomcat7 javascriptmvc


【解决方案1】:

您的 Spring MVC 调度程序 servlet 必须在 / 注册正确 -

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

如果是这样,也将&lt;mvc:default-servlet-handler /&gt; 添加到您的上下文中,这会将对静态资源的调用分派给容器的默认 servlet,而不是 Spring 尝试处理它

此外,使用这样的绝对路径引用您的资源:

<script src='${pageContext.request.contextPath}/WebContent/javascriptmvc/steal/steal.production.js'/>

【讨论】:

  • 您能否澄清一下“如果是这样,请在您的上下文中添加一个 ”,还请告诉我应该在哪个文件中添加这个 mvc 标签。
【解决方案2】:

问题已经解决了,感谢各位大侠的回复...... 我在我的 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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"   
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:oxm="http://www.springframework.org/schema/oxm"
       xsi:schemaLocation="http://www.springframework.org/schema/oxm    
    http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />

然后在jsp文件中我添加了这个来加载js文件

&lt;script type='text/javascript' src="${pageContext.request.contextPath}/resources/javascriptmvc/steal/steal.production.js"&gt;&lt;/script&gt;

我也稍微改变了目录结构

-webapps
    -WEB-INF
        -web.xml
        -mvc-dispatcher0servlet.xml
        -pages
            -hello.jsp
    -resources
        -javscriptmvc
            -steal
                -steal.production.js

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2011-05-15
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2012-02-02
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多