(二)搭建Spring MVC开发环境

        在创建王项目并且为其添加完Spring开发支持后,会自动提供有一个SpringMVC的相关开发包


    就可以直接在项目之中使用Spring MVC了.

    既然是Spring MVC,那么现在就必须为其定义相关的配置.

1.Spring MVC的所有配置都要求在applicationContext.x.ml文件之中定义.

范例:修改applicationContext.xml文件

        

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
		<!-- Spring MVC也是基于Annotation实现的配置 -->
		<context:annotation-config />
		<context:component-scan base-package="cn.zwb"/>
		<!-- 定义Spring MVC的处理 -->
		<mvc:annotation-driven/>
		<mvc:default-servlet-handler/>
		
</beans>

2.配置web.xml文件

        (Struts2.x它在进行设计的时候重点只是关注了与Struts1.x的区别,所以在整个的处理过程之中,它必须利用过滤器来进行Action的行为触发,但是这种做法并不好用,因为还是传统的过滤器会比较方便.)

        最好的MVC设计,所有的控制器的处理依然使用Servlet完成.而SpringMVC里面就支持Servlet的控制处理;

        Servlet类:org.springframework.web.servlet.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>SpringMVCProject</display-name>
  <!-- 此部分的操作时负责整个Spring容器启动的,即便使用了Spring MVC也不能够缺少此配置 -->
  <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>
  <!-- 配置Spring MVC中要使用的控制器 -->
  <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>
        此处使用了Servlet进行所有Action的分配处理,那么也就证明,在开发之中,过滤器可以实现所有的验证操作了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值