springMVC学习三 注解开发环境搭建

SpringMVC注解开发环境搭建,和之前的配置文件搭建,只有在springMVC配置文件的地方不一样,在配置DispatcherServlet时是一样的

springMVC的配置如下;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd 
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd "> 

    <!-- 开启扫描注解的包
        注意,此处的处理器所在的包要在springMVC的配置文件中进行扫描,不能在spring的配置文件中进行
        扫描,因为处理器要注册到springMVC容器中
     -->
    <context:component-scan base-package="com.caopeng.controller"></context:component-scan>
    <!-- 注解驱动 -->
    <!-- 
        上面的注解相当于下面两个类
        org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
     -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:annotation-config />
    <!-- 放行静态资源,不拦截静态资源 -->
    <!-- /js/* 代表项目下的js文件夹下的所有的文件
        /js/js** 代表项目下的js文件夹下的所有文件以及子文件夹下的所有文件
        
        下面代表:只要发现请求路径符合/js/**格式,就到/js/下去找资源
        mapping的值是请求的格式,location的值是资源位置
        
     -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    
    <!-- 注册  视图解析器 
        有时候,我们为了保护页面不被别人访问,可以把页面放在WEB-INF中,
        就可以把prefix配置成"/WEB-INF/"
        【注意】视图解析器是解析处理器最后的return 的值,并非我们在前端自己输入的请求
        如果return 的视图有前缀(forward或者redirect),视图解析器用默认的,如果没有前缀,则用我们自己配置的
    -->
    <bean id="viewResolver" class=" org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=""></property>
    </bean>
</beans>

猜你喜欢

转载自www.cnblogs.com/cplinux/p/9783842.html