Spring mvc资源不起作用

时间:2014-11-18 11:42:14

标签: spring spring-mvc java-ee

我使用Eclipse Luna创建了一个简单的Spring MVC项目,但是如果我在springConfig-servlet.xml中添加mvc:resources,它就无法工作。我在Tomcat中有这个错误:

 PM org.springframework.web.servlet.PageNotFound noHandlerFound
Avvertenza: No mapping found for HTTP request with URI [/SpringStore/] in   DispatcherServlet with name 'springConfig'

我在哪里做错了?谢谢

这是我项目的结构

enter image description here

Web.xml代码

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringStore</display-name>

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

<servlet-mapping>
  <servlet-name>springConfig</servlet-name>
   <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app>

springConfig-Servlet代码:

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<context:component-scan base-package="controller"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>

HomeController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value="/",method=RequestMethod.GET)
public String welcome(){
    return "index";     
}//welcome

}

文件index.jsp的内容很长,我不在这里写。

1 个答案:

答案 0 :(得分:6)

我认为您缺少 springConfig-Servlet.xml 文件中的条目。

<mvc:default-servlet-handler />
<mvc:annotation-driven />
相关问题