Spring实战学习笔记-SpringMVC

使用idea搭建spring项目

idea创建spring项目

需要idea ultimate版本,使用社区版本不支持spring开发
Spring实战学习笔记-SpringMVC_第1张图片

项目支持maven

Spring实战学习笔记-SpringMVC_第2张图片

pom.xml常用配置

  1. spring相关

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-coreartifactId>
    <version>4.3.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.3.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>4.3.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webartifactId>
    <version>4.3.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>4.3.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-aopartifactId>
    <version>4.3.8.RELEASEversion>
dependency>
  1. junit测试相关包

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
dependency>


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>4.3.8.RELEASEversion>
dependency>
<dependency>
    <groupId>com.github.stefanbirknergroupId>
    <artifactId>system-rulesartifactId>
    <version>RELEASEversion>
dependency>
  1. web开发tablib包

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jstlartifactId>
    <version>1.2version>
dependency>
  1. 其它包

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
dependency>

基本代码解析

AbstractAnnotationConfigDispatcherServletInitializer

  1. 类结构图

Spring实战学习笔记-SpringMVC_第3张图片

  1. 初始化流程

在AbstractContextLoaderInitializer.onStartup()函数中初始化createRootApplicationContext(),
关注的getRootConfigClasses就是这步进行注册的。
在AbstractDispatcherServletInitializer.onStartup()函数中初始化createServletApplicationContext(),
getServletConfigClasses在这步注册上的。

自定义JavaConfig

需要继承AbstractAnnotationConfigDispatcherServletInitializer,并实现getRootConfigClasses(),
getServletConfigClasses()和getServletMappings()三个函数

代码如下:

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Created by Administrator on 2017/5/30.
 */
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { RootConfig.class};
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[] { WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

RootConfig的实现:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * Created by Administrator on 2017/5/30.
 */
@Configuration
@ComponentScan(basePackages={"spittr"},
                excludeFilters = {
        @ComponentScan.Filter(type= FilterType.ANNOTATION, value= EnableWebMvc.class)
                })
public class RootConfig {
}

WebConfig的实现:

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Created by Administrator on 2017/5/30.
 */
@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    public void configureDefaultServletHandler(
            DefaultServletHandlerConfigurer configurer
    ) {
        configurer.enable();
    }
}

业务逻辑实现

  1. Controller的实现
package spittr.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by Administrator on 2017/5/30.
 */
@Controller
public class HomeController {
    @RequestMapping(value = "/", method = GET)
    public String home() {
        System.out.println("get request!!!");
        return "home";
    }
}
  1. view的实现
    编辑home.jsp,并存放到WEB-INF/views/home.jsp下
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>

<html>
  <head>
    <title>Spittrtitle>
    <link rel="stylesheet"
          type="text/css"
          href="" >
  head>
  <body>
    <h1>Welcome to Spittrh1>
    <a href="">Spittlesa>  |
    <a href="">Registera>
  body>
html>

跟踪Spring MVC的请求

  1. 请求发送到DispatcherServlet
  2. DS(DispatcherServlet)发送到处理器映射
    根据url查找Controller
  3. DS发送到控制器
    Controller处理业务逻辑
  4. 控制器经过 模型及逻辑视图名 返回到DS
    这一步根据Controller返回值, 包括模型和视图名
  5. DS发送到视图解析器
    根据视图名查找到对应的jsp文件
  6. DS发送到视图

你可能感兴趣的:(java)