大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错
Posted 挖坑大王
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错相关的知识,希望对你有一定的参考价值。
创建了一个空的SpringMVC项目,Tomcat可以正常启动,但是运行的时候,始终进不了Controller,并且报404错误。绝大多数是因为扫描包没配置好
eg:
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" version="3.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc-servlet.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--默认注解映射的支持 --> <mvc:annotation-driven /> <!--自动扫描目录下所有的类文件 --> <context:component-scan base-package="com.myspringmvc.controller.*" /> <!--对模型视图名称的解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
TestController.java
package com.myspringmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class TestController { @RequestMapping(value="/test",method={RequestMethod.GET,RequestMethod.POST}) public String test() { return "test"; } }
访问页面:
解决方案如下:
<!--自动扫描目录下所有的类文件 --> <context:component-scan base-package="com.myspringmvc.controller.*" /> ----改为---->>>> <!--自动扫描目录下所有的类文件 --> <context:component-scan base-package="com.myspringmvc.controller" />
以上是关于大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错的主要内容,如果未能解决你的问题,请参考以下文章
项目实战:Qt+OpenCV大家来找茬(Qt抓图,穿透应用,识别左右图区别,框选区别,微调位置)
想把在Windows下的jsp项目部署到Linux下,已经在linux下装好了tomcat,且能正常启和访问,但把jsp项目