springMVC学习(二)

    基于springMVc注解配置

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" 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>spring-mvc1</display-name>

  <servlet>

   <servlet-name>smvc</servlet-name>

               <!-- 把请求交给前端控制器DispatcherServlet  他再去找下面配置好的init-param 最终找到 /WEB-INF/classes/*-      servlet.xml -->

   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

   <init-param>

   <!-- contextConfigLocation配置springmvc加载的配置文件()配置处理器映射器,适配器 -->

   <!-- 如果不配置,默认加载的是/WEB-INF/名称-servlet.xml -->

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

   <param-value>/WEB-INF/classes/*-servlet.xml</param-value>

   </init-param>

   <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

              <!-- 首先服务器启动,会第一时间找到web.xml,通过url-patern拦截所有的请求,找到上面的smvc -->

   <servlet-name>smvc</servlet-name>

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

  </servlet-mapping>

  <welcome-file-list>

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

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

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

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

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

 

----------------------------------------------------------------------------------------------------------------------

springMVC-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

xsi:schemaLocation="

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

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

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

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

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

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

           

            <!-- 配置处理器适配器 ,可省略-->

    <!-- 配置处理器映射器,可省略-->

    <!-- 配置视图解析器 -->

<!--如下注入的所有属性,如果不需要可以一个都没有。,可省略-->

<!--   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->

<!-- 如果要返回的所有的视图都在同一个package下,比如都在/WEB-INF下,可以使用同一的前缀 -->

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

<!-- <property name="suffix" value=".jsp"></property>-->

<!--</bean>  -->

    <!-- 配置处理器 

name:代表了将来用户访问时,指明的urlname值一样时,访问class所指定的对象。

name的值需要带上路径信息,/不能省略

-->

<!--否则静态资源不能访问  --> 

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

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

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

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

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

<!--默认扫描的包路径,使用注解后 -->

<context:component-scan base-package="com.briup.run.web.controller"></context:component-scan>

<!-- 添加注解驱动,定义了很多默认配置 这个很强大-->

<mvc:annotation-driven></mvc:annotation-driven>

<!-- <import resource="classpath:spring_service.xml"/> -->

</beans>

 

     上面的处理器映射器,处理器适配器和视图解析器都可以不配置,是因为springMVC有默认的

                                                        在我们导入的jar包中

 

DispatcherServlet.properties这个文件中有配置好的各种解析器 映射器 适配器

但是有个适配器和映射器现在已经过期了,所以我们就使用注解驱动

<mvc:annotation-driven></mvc:annotation-driven>

 

下面是springMVC.xml配置最简单的内容

<!--否则静态资源不能访问  --> 

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

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

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

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

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

<!--默认扫描的包路径,使用注解后 -->

<context:component-scan base-package="com.briup.run.web.controller"></context:component-scan>

<!-- 添加注解驱动,定义了很多默认配置 这个很强大-->

<mvc:annotation-driven></mvc:annotation-driven>

 

posted @ 2017-03-16 15:43  helloword.java  阅读(117)  评论(0)    收藏  举报