Web.xml拦截所有:
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
spring-servlet.xml配置:
<mvc:resources mapping=”/css/**” location=”/css/” /> <mvc:resources mapping=”/images/**” location=”/images/” /> <mvc:resources mapping=”/ckeditor/**” location=”/ckeditor/” /> <mvc:resources mapping=”/js/**” location=”/js/” /> <mvc:resources mapping=”/slide_img/**” location=”/slide_img/” />
<!– 启用spring mvc 注解–><context:annotation-config />
<!– 设置使用注解的类所在的jar包 –> <context:component-scan base-package=”com.zkl.controller”></context:component-scan>
<!– 完成请求和注解POJO的映射 –> <bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter” />
<!– 对模型视图名称的解析,在请求时模型视图名称添加前后缀 –> <bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name=”prefix” value=”/” /> <property name=”suffix” value=”.jsp” /> <property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView” /> </bean>
就这配置,其他的都好好的,要考注解实现。
我直接访问一个jsp然后跳转到我的Controller发现直接404了。
然后我把拦截改为/*发现不404。可以直接访问项目,也就是index页面。但是不能直接访问到jsp了。
说明 /*是拦截所有包括.jsp后缀。/不包含jsp。
我又将mvc:resources的标签全部去掉,发现都正常了,但是访问不到图片了。说明图片以及css和其他静态资源被拦截了。
于是我去检查其他地方,我猜测是spring-servlet.xml的问题。我进去后就找到了<context:annotation-config />这个标签。这个是启用注解的。
我百度了一番后发现配置这个的并不多 <mvc:annotation-driven /> 倒都是配置着。
我找到了这么一句话:
<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。 <context:annotation-config/>1)隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。在配置文件中使用<context:annotationconfig/>之前,必须在 <beans> 元素中声明 context 命名空间<context:component-scan/>。2)是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor两个Processor的功能(例如JPA等)否则就可以将 <context:annotation-config/> 移除了。
原文链接:http://www.cnblogs.com/yangqian/archive/2013/02/01/2888753.html