Spring MVC - 找不到HTTP请求的映射(用于资源)

时间:2017-03-15 23:27:59

标签: java spring eclipse jsp spring-mvc

在eclipse中我是following Spring MVC project,但是当我在服务器上运行时,我对资源负载(图像,CSS,js ......)有疑问。 我添加了mvc:resources mapping,但是当我运行jsp时,eclipse告诉我:

  

org.springframework.web.servlet.DispatcherServlet noHandlerFound   在DispatcherServlet中找不到带有URI [/ProgettoTecWeb/<c:url value=]的HTTP请求的映射,其名称为&#39; dispacciatore&#39;

你有解决方案吗?感谢&#39;!小号

的web.xml

<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Talent Show</display-name>

   <servlet>
      <servlet-name>dispacciatore</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

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

</web-app>

dispacciatore-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">     <context:annotation-config />

    <context:component-scan base-package="it.uniparthenope"/>
    <!-- Vado a settare il package dove andrò a mettere la mia classe java
    che farà da home Controller. -->            
   <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:view-controller path="/" view-name="index"/>


    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
      <mvc:annotation-driven />

</beans>

index.jsp (如何调用资源)

<link href="<c:url value='/resources/assets/css/style.css"/>" rel="stylesheet">
...
    <script src="<c:url value="/resources/assets/js/custom.js"/>"></script>

3 个答案:

答案 0 :(得分:0)

您需要为Controller类创建包,使用注释RequestMapping连接到http映射。最后,使用组件扫描指向特定的Controller包。它应该工作。

答案 1 :(得分:0)

看起来你在从index.jsp调用资源时有一个错字。这可能也是问题

注意引号

您目前正在这样打电话

<link href="<c:url value='/resources/assets/css/style.css"/>" rel="stylesheet">

而不是

<link href="<c:url value="/resources/assets/css/style.css"/>" rel="stylesheet">

看看它是否有帮助。 你实际上可以给出没有jstl标签的路径,它在春天工作得很好

答案 2 :(得分:0)

  1. 项目 - &gt;构建路径 - &gt;配置构建路径 - &gt;添加服务器运行时
  2. 将运行的Java版本更改为最新版本
  3. Windown - &gt;显示视图 - &gt;问题 - &gt;快速修复所有问题