学习springMVC遇到的配置问题!!

来源:互联网 发布:知乎学堂错题本 编辑:程序博客网 时间:2024/06/05 14:14

spring 未扫描controller,提示路径未找到

实验了一天终于正确的,贴在博客作为模板

web.xml

<!--标签报错  通常是头文件少了东西--><?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <servlet>    <servlet-name>mvc-dispatcher</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!--DispatcherServlet对应的上下文配置,默认为/WEB-INF/$servlet-name$-servlet.xml,下面语句改变了该默认参数      -->    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath*:config/mvc-dispatcher-servlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>mvc-dispatcher</servlet-name>    <url-pattern>/</url-pattern>    <!--拦截所有访问-->  </servlet-mapping><!--加入Spring applicationContext 自定义路径,进行加载--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/applicationContext.xml</param-value></context-param>  <!--使用默认参数WEB-INF路径下的application.xml文件 则不需要监听--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--ContextLoaderListener 监听启动tomcat时 加载上下文--></web-app>

mvc-dispatcher-servler.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- 启用spring mvc 注解 -->    <!-- 本配置文件是工名为mvc-dispatcher的DispatcherServlet使用, 提供其相关的Spring MVC配置 -->    <!-- 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。 激活 @Required         @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->    <context:annotation-config /><!-- DispatcherServlet上下文, 只管理@Controller类型的bean, 忽略其他型的bean, 如@Service -->    <!--base-package 设置使用注解的类所在的jar包 -->    <context:component-scan base-package="com.imooc.mvcdemo">        <context:include-filter type="annotation"            expression="org.springframework.stereotype.Controller" />    </context:component-scan>    <!--扩充注解驱动,可将参数绑定到控制器参数-->    <mvc:annotation-driven/><!-- 静态资源处理, css, js, imgs -->    <!--<mvc:resources mapping="/resources/**" location="/resources/" />-->    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /></beans>

spring 的 applicationContext.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:context="http://www.springframework.org/schema/context"       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">    <context:annotation-config />    <!--  base-package 注解所在的包根据自己的需要划分注解类使用的范围  -->    <context:component-scan base-package="main.com.talkweb">        <!--不再管理Controller 注解  因为它单独给mvc-servler.xml去管理-->        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />    </context:component-scan></beans>

工程目录
这里写图片描述

如果还是访问不了 而且是 404错误,看看工程的web
这里写图片描述

看看自己的tomcat
这里写图片描述