入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

spring mvc中设置登录拦截器,报错:元素 "mvc:interceptors" 的前缀 "mvc" 未绑定。

创建时间:2016-05-29 投稿人: 浏览次数:3944

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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    
<!--登录控制拦截器-->  
<mvc:interceptors>  
  <mvc:interceptor>   
     <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/login.do"/>  
    <bean class="org.bjlx.base.login.AuthInterceptor"></bean>   
  </mvc:interceptor>   
</mvc:interceptors>   
 
</web-app>

报错:

元素 "mvc:interceptors" 的前缀 "mvc" 未绑定。

解释:

1先理解一下这个<mvc:interceptors>:

其中mvc是命名空间interceptors是元素。那么xml文件为什么要引入命名空间呢?

详细参考http://www.w3school.com.cn/xml/xml_namespaces.asp。因为如果两个xml文件(如A.xml,B.xml)中都有某个元素<table>

这个 A.xml文档携带着某个表格中的信息:
<table>
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>
这个 B.xm 文档携带有关桌子的信息(一件家具):
<table>
   <name>African Coffee Table</name>
   <width>80</width>
   <length>120</length>
</table>

而如果将这两者xml文档合并(假如合并成一个.html文件c.html),由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。这时候需要对A.xml和B.xml各自给一个命名空间(一般用一个全球唯一的网址表示,如A.xml命名空间为http://www.A.com/,B.xml命名空间为http://www.B.com),这时候就算是合并A.xml和B.xml也不存在问题。

c.html文件如下(其中下文紫色的部分xmlns="http://www.w3.org/1999/xhtml",表示c.html这个文件的命名空间是http://www.w3.org/1999/xhtml):

<html xmlns="http://www.w3.org/1999/xhtml">
xmlns:a="http://www.A.com/"

xmlns:b="http://www.B.com"

>

<a:table>
   <a:tr>
   <a:td>Apples</a:td>
   <a:td>Bananas</a:td>
   </a:tr>
</a:table>
<b:table>
   <b:name>African Coffee Table</b:name>
   <b:width>80</b:width>
   <b:length>120</b:length>
</b:table>

html的命名空间,可以参考http://www.w3school.com.cn/tags/tag_prop_xmlns.asp

导致问题的原因

这是因为

1mvc的命名空间和xsd文件,以及以下部分是在spring.xml配置文件中的!!不是在web.xml

<!--登录控制拦截器-->  
<mvc:interceptors>  
  <mvc:interceptor>   
     <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/login.do"/>  
    <bean class="org.bjlx.base.login.AuthInterceptor"></bean>   
  </mvc:interceptor>   
</mvc:interceptors>   

2诸如<mvc:interceptors>中mvc这样的元素,没有在web.xml文件的<web-app>中声明mvc是那个命名空间的简化替代词(就像上面例子中的,<a:tr>中,a是http://www.A.com的替代,这样就不用每次都写<http://www.A.com:tr>,而是<a:tr>即可)。

3.<mvc:interceptors>中interceptors元素,在当前引用的所有schema即.xsd文件中,没有interceptors元素的定义,所以web.xml不能使用interceptors元素!需要引入定义了interceptors元素的xsd文件。

4我一开始的spring mvc是2.5版本的,spring mvc2.5版本根本没有mvc标签,要spring mvc 3.0版本以上才有。另外xsd文件放在jar包的什么问题,参考:

http://www.tuicool.com/articles/nIFJfm。

解决办法:

1换用spring mvc 4.04版本的所有jar包。

2在spring.xml文件的<web-app>中声明mvc是那个命名空间的简化替代词,在web.xml的<web-app>中加入xmlns:mvc="http://www.springframework.org/schema/mvc"。

3引入定义了interceptors元素的xsd文件,并且不要配置xsd的版本。至于为什么在Spring的配置里,最好不要配置xsd文件的版本号,参考:http://zhidao.baidu.com/link?url=DbEvGBOvGAhfsN5dx1Bw5-K_aJaIuS10A9wE4HLB6ClQqGQRarZHQztQSmagAfR5a-ou1Xnp5MykKsxRYKxkIWesYqOJNPKD3dg_-LP5HeC

spring.xml配置文件如下;

<?xml version="1.0" encoding="UTF-8"?>

<!--spring-mvc.xsd文件,不要配置xsd的版本-->
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           <!--登录控制拦截器 -->
<mvc:interceptors>
   
<mvc:interceptor>
<mvc:mapping path="/**" />
<!--  
<mvc:exclude-mapping path="/login.do" />
-->
<bean class="org.bjlx.base.login.AuthInterceptor"></bean>
</mvc:interceptor>

</mvc:interceptors>

</beans>


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。