【问题标题】:HTTP 404 when linking dojo/dojo.js script src in Spring MVC在 Spring MVC 中链接 dojo/dojo.js 脚本 src 时的 HTTP 404
【发布时间】:2014-04-12 03:06:02
【问题描述】:

好的,所以一段时间以来,我一直在尝试设置与 DOJO 集成的 Spring MVC 项目。终于决定是时候在这里做一个帐户并提出问题了。除了我从标签引用的 dojo.js 文件之外,我已经加载了所有内容。我将提供我的项目层次结构和我所看到的:

我的@RequestMapping 方法。

@RequestMapping(value = "/rbd", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "home";
    }

..当我浏览到http://localhost:8080/myproject/rbd 时,应用程序正在成功加载 home.jsp 文件,如下所示:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<%@ page session="false"%>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />

<link rel="icon" href="../sba-icon.png" type="image/x-icon" />
<link rel="shortcut icon" href="../sba-icon.png" type="image/x-icon" />

<script src="<spring:url value='/scripts/dojo/dojo.js'/>" data-dojo-config:"locale:'en', async:true, parseOnLoad:false"></script>

..我的 JBOSS 部署文件夹遵循 myproject/scripts/dojo/dojo.js 的层次结构。我的 home.jsp 在 myproject/WEB-INF/views/home.jsp 中。 src 标记的位置是否相对于 home.jsp 的位置?我很迷茫,因为我不断收到 404 错误。但我显然知道 dojo.js 在服务器上。我已经尝试了每条对我有意义的路径,我是 Spring/MVC 初学者,所以请不要抨击!

感谢您的帮助! :)

编辑:这是我在 web.xml 中的 DispatcherServlet 的初始化 ...

<servlet>
        <servlet-name>rbd</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/rbd/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

..这是我的 servlet-context.xml,位于上面显示的路径中..

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

【问题讨论】:

  • 您的DispatcherServlet 映射到什么位置?
  • 抱歉 Sotirios,我是 Spring 新手,所以我不确定你在问什么。我使用帖子底部的 DispatcherServlet 信息编辑了原始帖子。
  • 由于您有一个映射到/resources 的资源处理程序,请将您的scripts 放入/resources 并相应地访问它。
  • 不确定您的意思。我在资源文件夹中链接了我构建的脚本文件夹,仍然得到 404
  • edit:: 所以我将映射更改为 然后我不再得到 404!还有更多问题需要解决,但我想这对于第一次项目设置来说是正常的

标签: html spring model-view-controller dojo http-status-code-404


【解决方案1】:

问题是您没有映射到您的/scripts 目录。使用 Spring MVC 时,每个请求都被映射到某些东西。您已映射以下内容:

  • /rbd:在您的控制器中
  • /resources:在您的servlet-context.xml 中(作为资源)

正如您所见,/scripts 目录没有任何映射,因此为了使其正常工作,您应该将其移动到资源文件夹(并调整链接)或提供一些额外的映射,通过添加以下是您的配置:

 <resources mapping="/scripts/**" location="/scripts/" />

没有它,当您尝试访问 Dojo 时,Spring 根本不知道它应该指向哪里。

【讨论】:

    【解决方案2】:

    DispatcherServlet 拦截js文件
    你应该设置&lt;servlet-mapping&gt; 喜欢:

       <servlet-mapping>
        <servlet-name>rbd</servlet-name>
        <url-pattern>*.do</url-pattern>
       </servlet-mapping>
    

    同时更改您的控制器映射:

    @RequestMapping(value = "/rbd.do", method = RequestMethod.GET)
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2014-07-15
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2017-04-13
      相关资源
      最近更新 更多