Js not found spring mvc

Js not found spring mvc

本文关键字:mvc spring found not Js      更新时间:2023-09-26

大家好,我在jsp中发现了一个js文件问题,我有一个警告

 <script src="<c:url value="bower_components/jquery/dist/jquery.min.js"/>"></script>
<script src="<c:url value="bower_components/bootstrap/dist/js/bootstrap.min.js"/>"></script>
<script src="<c:url value="bower_components/metisMenu/dist/metisMenu.min.js"/>"></script>
<script src="<c:url value="dist/js/sb-admin-2.js"/>"></script>

我的jsp无法访问js文件

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

警告:org.springframework.web.servlet.PageNotFound-在名为"appServlet"的DispatcherServlet中找不到URI为[/bap/bower_components/jquery/dist/jquery.min.js]的HTTP请求的映射

有人能告诉我出了什么问题吗,谢谢

首先让我们了解<mvc:resources mapping="/resources/**" location="/resources/" />

在这里,您正在配置springMVC的DispatcherServlet,以将具有模式/resources/**的所有HTTP请求映射到物理目录/resources/。这样做是为了将csjsimages等资源放入文件夹webapp/resources中,并用作web应用程序的静态内容。参考:Spring MVC-在JSP页面中包含JS或CSS文件

现在,你可以通过几种方式解决你的问题,其中一种解释如下:

  • WebContent中创建一个名为resources的目录。将所有静态内容移动到其各自的目录中,例如将jquery.min.js移动到WebContent/resources/bower_components/jquery/dist/jquery.min.js,对于其他目录也是如此
  • /resources/附加到url。在JSP页面中,访问静态内容

<script src="<c:url value="/resources/bower_components/jquery/dist/jquery.min.js"/>"></script>

我希望这能帮助到你,请随时评论以获得进一步的帮助!