首页 > 代码库 > spring MVC (学习笔记)

spring MVC (学习笔记)

技术分享

技术分享

web.xml 相关配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
  <display-name>Archetype Created Web Application</display-name>

 

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- DispatcherServlet对应的上下文配置,默认为/WEB-INF/$servlet-name$-servlet.xml -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!-- dispatcher拦截所有请求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

对应的springmvc 的相关配置

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
  <!-- 配置spring -->
  <!-- 1:开启SpringMVC注解模式 -->
  <!-- 简化配置:
    (1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
    (2)提供一系列:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,xml,json默认读写支持
  -->
  <mvc:annotation-driven/>
  <!-- 2:servlet-mapping 映射路径:"/" -->
  <!-- 静态资源默认servlet配置
    1.加入对静态资源的处理
    2.欲需使用"/"做整体映射
  -->
  <mvc:default-servlet-handler/>
  <!-- 3:配置jsp 显示ViewResolver -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp/"/>
    <property name="suffix" value="http://www.mamicode.com/.jsp"/>
  </bean>
  <!-- 4:扫描web相关的bean -->
  <context:component-scan base-package="com.cgj.spring.mvc.controller">
    <!-- 只扫描@Controller标注的类,不扫描其他标注的类 -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
</beans>

 

spring MVC (学习笔记)