springmvc 无法扫描到controller层

各种404,请求/user/showUser不能进入controller,整了一天,快疯了,求组各位大神!直接上代码
项目结构:
clipboard.png

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<display-name>Archetype Created Web Application</display-name>

<!-- Spring 配置文件路径,此处可将Spring MVC的相关配置内容配置到Spring的配置文件applicationContext.xml中,共享同一个配置文件即可 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- Spring 监听器 配置 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

</listener>

<!-- 字符集 过滤器 -->

<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>

<!-- Spring mvc 配置,配置文件名称默认为{servlet-name}-servlet.xml,路径默认在/WEB-INF/下 -->

<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:springmvc/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>

<url-pattern>/</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

applicationContent.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.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.0.xsd">

<mvc:annotation-driven />

<!-- 自动扫描 -->

<

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

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan

<!-- 引入配置文件 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location" value="classpath:dataSource.properties" />

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="${driver}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<!-- 初始化连接大小 -->

<property name="initialSize" value="${initialSize}"></property>

<!-- 连接池最大数量 -->

<property name="maxActive" value="${maxActive}"></property>

<!-- 连接池最大空闲 -->

<property name="maxIdle" value="${maxIdle}"></property>

<!-- 连接池最小空闲 -->

<property name="minIdle" value="${minIdle}"></property>

<!-- 获取连接最大等待时间 -->

<property name="maxWait" value="${maxWait}"></property>

</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!-- 自动扫描mapping.xml文件 -->

<property name="mapperLocations" value="classpath:mybatisMappingConfig/*.xml"></property>

</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.chs.dao" />

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

</beans>

spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 添加注解驱动 -->

<mvc:annotation-driven />

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->

<context:component-scan base-package="com.chs.controller"

use-default-filters="false">

<context:include-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<!--避免IE执行AJAX时,返回JSON出现下载文件 -->

<bean id="mappingJacksonHttpMessageConverter"

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->

<bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<list>

<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->

</list>

</property>

</bean>

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="order" value="10"></property>

<property name="prefix" value="/WEB-INF/page/"></property>

<property name="suffix" value=".html"></property>

<property name="contentType" value="text/html;charset=utf-8"></property>

</bean>

<!-- ===================================================== -->

<!-- ViewResolver For FreeMarker -->

<!-- ===================================================== -->

<bean id="freemarkerResolver"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<property name="order" value="0" />

<property name="suffix" value=".html" />

<property name="contentType" value="text/html;charset=utf-8" />

<property name="viewClass">

<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView

</value>

</property>

</bean>

<!-- ===================================================== -->

<!-- ViewResolver For FreeMarkerConfigurer -->

<!-- ===================================================== -->

<bean id="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<property name="templateLoaderPath">

<value>/WEB-INF/page/</value>

</property>

<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性 -->

<props>

<prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->

<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->

<prop key="locale">UTF-8</prop><!-- 本地化设置 -->

<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>

<prop key="time_format">HH:mm:ss</prop>

<prop key="number_format">0.####</prop>

<prop key="boolean_format">true,false</prop>

<prop key="whitespace_stripping">true</prop>

<prop key="tag_syntax">auto_detect</prop>

<prop key="url_escaping_charset">UTF-8</prop>

</props>

</property>

</bean>

<!-- 处理静态资源 -->

<mvc:resources mapping="/css/**/" location="/css/" />

<mvc:resources mapping="/imgages/**/" location="/imgges/" />

<mvc:resources mapping="/js/**/" location="/js/" />

<!-- 文件上传配置 -->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 默认编码 -->

<property name="defaultEncoding" value="UTF-8" />

<!-- 上传文件大小限制为31M,31*1024*1024 -->

<property name="maxUploadSize" value="32505856" />

<!-- 内存中的最大值 -->

<property name="maxInMemorySize" value="4096" />

</bean>

</beans>

controller

package com.chs.controller;

import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import com.chs.base.util.ReflectionUtil;

import com.chs.model.User;

import com.chs.service.IUserService;

@Controller

@RequestMapping("/user")

public class UserController {

@Resource

private IUserService userService;

@RequestMapping("/showUser")

public String toIndex(HttpServletRequest request,Model model){

System.out.println("------------------------");

Map<String, Object> paramMap = ReflectionUtil.po2Map(request);

User user = this.userService.getUserById(paramMap);

return "showUser";

}

}

回答:

会不会是jsp里的请求路径问题,建议试下改成绝对路径试下

回答:

可以参考下这个,希望能对你有所帮助:

https://github.com/pysasuke/s...

回答:

看下是不是忘了加项目名称了,如果默认不是ROOT项目,也没在tomcat中配置访问需要加上项目名称的

回答:

这样的路径前面应该是不用斜杠的,之前我也碰到过这个问题,

以上是 springmvc 无法扫描到controller层 的全部内容, 来源链接: utcz.com/p/179668.html

回到顶部