元素 "context:component-scan" 的前缀 "context" 未绑定的解决方案

在动态web项目(Dynamic Web Project)中,使用SpringMVC框架,新建Spring的配置文件springmvc.xml,添加扫描控制器

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

显示红线

此时启动tomcat显示如下错误,元素 "context:component-scan" 的前缀 "context" 未绑定

解决方案:

1、在springmvc.xml中添加context的声明,添加后红线消失

此时启动tomcat,显示如下错误:通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

2、在xsi:schemaLocation实例中引用模式文档,指出模式文档的位置

完整代码如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6     http://www.springframework.org/schema/beans/spring-beans.xsd
 7     http://www.springframework.org/schema/context  
 8     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9     <!-- 扫描器 -->
10     <context:component-scan base-package="com.bwlu.controller"/>
11     <!-- 视图解析器 解析jsp视图 默认使用JSTL标签 -->
12     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
13         <!-- 配置jsp路径的前缀 -->
14         <property name="prefix" value="/WEB-INF/views/" />
15         <!-- 配置jsp路径的后缀 -->
16         <property name="suffix" value=".jsp" />
17     </bean>
18 </beans>
原文地址:https://www.cnblogs.com/lixiang1993/p/7403324.html