RequestMappingHandlerMapping:219 - Did not find handler method for [/regist.do]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HeadingAlong/article/details/62230199
2017/03/15 16:37:39,083 DEBUG DispatcherServlet:823 - DispatcherServlet with name 'springDispatcherServlet' processing GET request for [/SSMproject/regist.do]
2017/03/15 16:37:39,085 DEBUG RequestMappingHandlerMapping:209 - Looking up handler method for path /regist.do
2017/03/15 16:37:39,085 DEBUG RequestMappingHandlerMapping:219 - Did not find handler method for [/regist.do]
2017/03/15 16:37:39,086  WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/SSMproject/regist.do] in DispatcherServlet with name 'springDispatcherServlet'
2017/03/15 16:37:39,086 DEBUG DispatcherServlet:946 - Successfully completed request
2017/03/15 16:37:39,087 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'sqlSessionFactory'

这个是RequestMappingHandlerMapping正在通过请求名匹配查找controller没有找到。
经过我一番查找
原先的配置着这样的(文章最后是我的解决方法

package com.hs.web.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hs.domain.common.User;
import com.hs.service.UserService;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("regist")
    public String regist(Model model,User user){
        User findUser = userService.findByUsername(user.getUsername());
        if(findUser==null){
            userService.save(user);
            model.addAttribute("message", "注册成功");
        }else{
            model.addAttribute("message", "用户名已经存在");
        }
        return "hello";
    }

    @RequestMapping("toregist")
    public String toRegist(){
        return "regist";
    }

}

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_2_5.xsd" version="2.5">
  <display-name>SSMproject</display-name>

  <!-- 配置spring容器监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

  <!-- 配置字符编码过滤器 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置前端控制器 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springmvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">


    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/user/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

spring.xml文件中内容这样写的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <context:component-scan base-package="com.hs"/>
    <!-- 加载属性配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.jdbcUrl}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- 配置sqlsessionfactory --> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    </bean>

    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置通知 -->
    <tx:advice id="txAdvicer" transaction-manager="transactionManager">
        <tx:attributes>
        <tx:method name="find*" propagation="REQUIRED"/>
        <tx:method name="get*" propagation="REQUIRED"/>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="insert*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor advice-ref="txAdvicer" pointcut="execution(* com.hs.service.*.*(..))"/>
    </aop:config>
    <!-- 打开全局注解扫描qi -->

    <!-- 批量配置mapper接口dao -->
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hs.dao"/>
    <!--    <property name="sqlSessionFactory" ref="sqlSessionFactory"/> -->
    </bean>


</beans>

bug改正之后的修改

其实就是一个扫描的配置写错了位置

<context:component-scan base-package="com.hs"/>

这行配置时必须要写在springmvc.xml文件中,因为加载该文件时,会扫描所有的加注解的controller,这样RequestMappingHandlerMapping才会找到这个对象

猜你喜欢

转载自blog.csdn.net/HeadingAlong/article/details/62230199