19-spring学习-springMVC环境配置

新建一共环境,添加spring支持,就可以开发springMVC了。

既然是springMVC,就必须为其定义相关配置。

1,springMVC所有配置都需要在applicationContext.xml中定义。

范例:修改配置文件

添加这几个支持:

 发现配置中已经支持了

对springMVC进行annotation的配置

    <!-- SpringMVC也是基于Annotaion实现的配置,启用annotation -->
    <context:annotation-config/>    <!-- 支持annotation -->
    <context:component-scan base-package="com.SpringMVC"/>        <!-- 指定扫描包 -->
    
    <!-- 定义springMVC处理 ,表示springMVC中将支持annotation-->
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

2,配置web.xml文件

最好MVC设计,所有的控制器依然使用Servlet完成,而springMVC支持支持Servlet处理。

Servelet类:DispatcherServlet

范例:修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC</display-name>
  
  <!-- 此部分的操作是负责Spring容器启动的,即便使用springMVC也不能缺少此配置 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置springMVC之中要使用的控制器 -->
  <servlet>
      <servlet-name>SpringMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
</web-app>

此处使用了servelet进行了所有action的处理,那么也就证明了,在开发中,过滤器可以实现所有验证操作。

 

 

 

  

 

posted @ 2018-01-06 23:44  美好的明天  阅读(191)  评论(0编辑  收藏  举报