SpringMVC之入门案例

SpringMVC之入门案例

一 SpringMVC概要

1 Spring 为展现层提供的基于 MVC 设计理念的优秀的 • Web 框架,是目前最主流的 MVC 框架之一
2 Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架• Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请• 求的控制器,而无须实现任何接口。
3 支持 REST 风格的 URL 请求• 采用了松散耦合可插拔组件结构,比其他 MVC 框架更具• 扩展性和灵活性。

二 HelloWord案例

1 步骤:

① 加入jar包
② 在web.xml中配置DispatcherServlet
③ 加入SpringMVC的配置文件
④ 编写处理请求的处理器,并标识为处理器
⑤ 编写视图

2 实现

(1)加入jar包

– spring-aop-4.0.0.RELEASE.jar
– spring-beans-4.0.0.RELEASE.jar
– spring-context-4.0.0.RELEASE.jar
– spring-core-4.0.0.RELEASE.jar
– spring-expression-4.0.0.RELEASE.jar
– spring-web-4.0.0.RELEASE.jar
– spring-webmvc-4.0.0.RELEASE.jar

(2)编写web.xml
配置DispatcherServlet,DispatcherServlet默认加载/WEB-INF/.xml的spring配置文件,启动web层的spring容器。可以通过contextConfigLocation初始化参数自定义配置文件的位置和名称。



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMVC.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    
    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

web-app>

(3)配置SpringMVC的配置文件
① 配置自动扫描包
② 配置视图解析器:视图名称解析器:将视图逻辑 名解析为: /WEB-INF/pages/.jsp


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="springmvc">context:component-scan>
    
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/">property>
        <property name="suffix" value=".jsp">property>
    bean>
    
    <mvc:annotation-driven/>

    
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="springmvc.utils.StringToDateConverter"/>
            set>
        property>
    bean>

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

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"/>
    bean>

    
    <bean id="sysExceptionResolver" class="springmvc.exception.SysExceptionResolver">bean>

    
    <mvc:interceptors>
        
        <mvc:interceptor>
            
            <mvc:mapping path="/user/*"/>
            
            
            <bean class="springmvc.controller.interceptor.MyInterceptor1" />
        mvc:interceptor>
    mvc:interceptors>

    
    <mvc:annotation-driven conversion-service="conversionService"/>
beans>

(4)创建请求处理器

SpringMVC之入门案例_第1张图片

3 大致流程分析

(1)输入URL:发起helloWord.action请求
(2)被web.xml配置的前端控制器dispatcherServlet拦截
(3)根据springMVC.xml中配置的视图解析器解析得到实际的物理视图为/WEB-INF/view/success.jsp
(4)浏览器得到success.jsp的内容

你可能感兴趣的:(SpringMVC学习笔记,SpringMVC,入门案例)