最近初学springmvc,做了一个简单工程实现Conntroller加载,一直报错404,调试许久没找到问题,请求帮助,多谢各位了!
编程环境:win10x64+eclipse+Tomcat8.5
文件结构:
主要代码:
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>MyWeb</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 加载spring配置文件 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:springMvc.xml</param-value> 17 </context-param> 18 <!-- spring监听 --> 19 <listener> 20 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 21 </listener> 22 <!-- 配置springMvc --> 23 <servlet> 24 <servlet-name>springMvc</servlet-name> 25 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 26 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:springMvc.xml</param-value> 30 </init-param> 31 32 <load-on-startup>1</load-on-startup> 33 <async-supported>true</async-supported> 34 </servlet> 35 36 <servlet-mapping> 37 <servlet-name>springMvc</servlet-name> 38 <url-pattern>/</url-pattern> 39 </servlet-mapping> 40 41 </web-app>
HelloController.java
1 package com.hello; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class HelloController { 8 9 @RequestMapping("/success") 10 public String helloWorld() { 11 System.out.println("拦截"); 12 return "success"; 13 } 14 }
springMvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> 11 12 <mvc:annotation-driven /> 13 14 <context:component-scan base-package="com.hello"/> 15 16 <!-- 处理静态资源 --> 17 <mvc:default-servlet-handler/> 18 19 <!-- 配置视图解析器 --> 20 <bean 21 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/WEB-INF/jsp/"/> 23 <property name="suffix" value=".jsp"/> 24 </bean> 25 </beans>
调试图: