haidao3467
haidao3467
采纳率0%
2016-07-03 06:36 阅读 6.6k

maven springMvc 扫描不到注解controller

 <?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.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
    <context:component-scan base-package="cn.com" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <mvc:annotation-driven>
    <!--<mvc:async-support default-timeout="300000"/>-->
    <mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
      <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
          <constructor-arg value="UTF-8" />
      </bean>
      </mvc:message-converters>
    </mvc:annotation-driven> 

    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  

    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->  
    <bean id="multipartResolver"    
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
        <!-- 默认编码 -->  
        <property name="defaultEncoding" value="utf-8" />    
        <!-- 文件大小最大值 -->  
        <property name="maxUploadSize" value="10485760000" />    
        <!-- 内存中的最大值 -->  
        <property name="maxInMemorySize" value="40960" />    
    </bean>   

</beans>  
 <?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"> 
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/*.xml</param-value>
  </context-param>

  <!-- 编码过滤器 -->  
  <filter>  
      <filter-name>encodingFilter</filter-name>  
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
      <async-supported>true</async-supported>  
      <init-param>  
          <param-name>encoding</param-name>  
          <param-value>UTF-8</param-value>  
      </init-param>  
  </filter>  
  <filter-mapping>  
      <filter-name>encodingFilter</filter-name>  
      <url-pattern>/*</url-pattern>  
  </filter-mapping>

  <!-- Spring监听器 -->  
   <listener>  
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
   </listener>
   <!-- 防止Spring内存溢出监听器 -->  
   <listener>  
       <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
   </listener>

   <!-- Spring MVC servlet -->  
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:/spring/spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>

</web-app>

 package cn.com.base.controller;


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


/**
 * 
 * @author Mr.Xu
 * @date 
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 测试
     * @author Mr.Xu
     * @date 
     * @return
     */
    @RequestMapping(value = "/goLogin", method = { RequestMethod.POST,RequestMethod.GET })
    public String goUser() {
        return "login";
    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • devmiao devmiao 2016-07-03 10:53
    点赞 评论 复制链接分享
  • chen124307 chen124307 2016-07-04 08:15

    在 自动扫描 后增加如下配置 :context:annotation-config/

    点赞 评论 复制链接分享
  • wjw_allen wjw_allen 2020-12-01 16:38

    感谢。我是少了这一行

     

    点赞 评论 复制链接分享

相关推荐