前提:

1,已安裝JDK

2, 有Intellij IDEA

3, 已安裝Gradle

一分鐘實現步驟:

1,mkdir Spring-MVC;cd Spring-MVC
2,gradle init
3,edit build.gradle file

  1. /*
  2. * This build file was auto generated by running the Gradle 'init' task
  3. * by 'Administrator' at '16-10-2 下午8:42' with Gradle 3.1
  4. *
  5. * This generated file contains a commented-out sample Java project to get you started.
  6. * For more details take a look at the Java Quickstart chapter in the Gradle
  7. * user guide available at https://docs.gradle.org/3.1/userguide/tutorial_java_projects.html
  8. */
  9.  
  10. buildscript {
  11. repositories {
  12. jcenter()
  13. }
  14. dependencies {
  15. classpath 'org.akhikhl.gretty:gretty:+'
  16. }
  17. }
  18.  
  19. ext {
  20. springVersion = '4.3.3.RELEASE'
  21. }
  22.  
  23. //apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
  24. apply plugin: 'org.akhikhl.gretty'
  25.  
  26. apply plugin: 'java'
  27. apply plugin: 'war'
  28. apply plugin: 'eclipse'
  29. apply plugin: 'idea'
  30.  
  31. // In this section you declare where to find the dependencies of your project
  32. repositories {
  33. // Use 'jcenter' for resolving your dependencies.
  34. // You can declare any Maven/Ivy/file repository here.
  35. jcenter()
  36. mavenCentral()
  37. maven { url 'http://repo.spring.io/release' }
  38. }
  39.  
  40. // In this section you declare the dependencies for your production and test code
  41. dependencies {
  42. //core spring
  43. compile ("org.springframework:spring-context:$springVersion")
  44. compile ("org.springframework:spring-core:$springVersion")
  45. compile ("org.springframework:spring-webmvc:$springVersion")
  46.  
  47. // The production code uses the SLF4J logging API at compile time
  48. compile ("org.slf4j:slf4j-api:1.7.21")
  49.  
  50. // Declare the dependency for your favourite test framework you want to use in your tests.
  51. // TestNG is also supported by the Gradle Test task. Just change the
  52. // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
  53. // 'test.useTestNG()' to your build script.
  54. testCompile ("junit:junit:4.12")
  55. }

build gradle

4,mkdir src\main\java ; mkdir src\main\resources ; mkdir src\main\webapp
 mkdir src\main\webapp\WEB-INF
 mkdir src\test\java ; mkdir src\test\resources
5,將項目導入idea,由於gradlew idea這個命令會生成一些多餘的文件,不喜歡垃圾多餘的東西,故直接導入

6,Spring MVC本質上就是Servlet, 因此須要定義一個web.xml和一個Spring MVC的配置文件,在WEB-INF下面添加web.xml和spring-mvc-servlet.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
  5.  
  6. <servlet>
  7. <servlet-name>spring-mvc</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>spring-mvc</servlet-name>
  13. <url-pattern>/</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  14.  
  15. <mvc:annotation-driven></mvc:annotation-driven>
  16. <context:component-scan base-package="org.springframework.samples"/>
  17. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/views/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21.  
  22. </beans>

spring-mvc-servlet.xml

關於Spring MVC的配置文件,最簡的配置是申明p,context,mvc命名空間便可。上面p實際上是沒有用到的。

7,根據第6步的spring-mvc-servlet.xml的配置,還須要在WEB-INF文件夾下面建立一個views文件夾,而且新建文件home.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2016/10/2 0002
  5. Time: 21:53
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>Home</title>
  12. </head>
  13. <body>
  14. <h1>
  15. Hello world!
  16. </h1>
  17.  
  18. <P> The time on the server is ${serverTime}. </P>
  19. </body>
  20. </html>

home.jsp

8,HomeController.java

  1. package org.springframework.samples;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9.  
  10. import java.text.DateFormat;
  11. import java.util.Date;
  12. import java.util.Locale;
  13.  
  14. /**
  15. * Created by Administrator on 2016/10/2 0002.
  16. */
  17. @Controller
  18. public class HomeController {
  19. private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  20.  
  21. /**
  22. * . * Simply selects the home view to render by returning its name.
  23. * .
  24. */
  25. @RequestMapping(value = "/", method = RequestMethod.GET)
  26. public String home(Locale locale, Model model) {
  27. logger.info("Welcome home! The client locale is {}.", locale);
  28.  
  29. Date date = new Date();
  30. DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  31.  
  32. String formattedDate = dateFormat.format(date);
  33.  
  34. model.addAttribute("serverTime", formattedDate);
  35.  
  36. return "home";
  37. }
  38.  
  39. }

HomeController.java

9,

  1. gradlew tomcatRun

10, http://localhost:8080/Spring-MVC/

11,理論延伸,Spring MVC的Servlet一共有三個層次

  HttpServletBean,直接繼承於Java的HttpServlet, 將Servlet中配置的參數設置到相應的屬性,並不參與實際請求的處理

  FrameworkServlet,初始化WebApplicationContext,將不一樣類型的請求合併到processRequest方法中統一處理

  DispatcherServlet,初始化自身的9個組件,主要將請求交給doDispatch方法具體處理。哪9大組件呢?他們是:

    HandlerMapping,根據request找到相應的Handler(就是常說的Controller)和Interceptors.

    HandlerAdapter,共有3個方法,supports判斷是否能夠使用某Handler,handler方法具體用Handler幹活,getLastModified是用來獲取資源的Last Modified.

    HandlerExceptionResolver,根據異常設置ModelAndView.並轉交給render方法進行渲染。

    ViewResolver,將String類型的視圖名和Locale解析爲View類型的視圖

    RequestToViewNameTranslator,根據request獲取到視圖名,由於有些handler並無返回視圖名的。我想這個是實現404的最好組件了,一個spring mvc容器裏只能配置一個。

    LocaleResolver,根據request解析出Locale

    ThemeResolver,根據request解析出主題名

    MultipartResolver,用於處理上傳文件,將普通的request轉換成MultipartHttpServletRequest

    FlashMapManager,用於管理FlashMap的,而FlashaMap主要用於redirect中傳遞參數

Java配置

按Spring的發展趨勢,如今推薦使用Java配置,下面就讓咱們來拋棄xml而擁抱Java配置吧

前面1-5步同樣的。從第6步開始開始改變

6, 要拋棄web.xml,必需要在支持Servlet3.0+的服務器才行,實現只需擴展一下

  1. AbstractAnnotationConfigDispatcherServletInitializer
  1. public class SampleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2. @Override
  3. protected Class<?>[] getRootConfigClasses() {
  4. return new Class<?>[0];
  5. }
  6.  
  7. @Override
  8. protected Class<?>[] getServletConfigClasses() {
  9. return new Class<?>[]{SpringMVCConfig.class};
  10. }
  11.  
  12. @Override
  13. protected String[] getServletMappings() {
  14. return new String[]{"/"};
  15. }
  16. }

SampleWebAppInitializer

接下來使用IDEA的Alter +Enter, 自動生成SpringMVCConfig.java

  1. @Configuration
  2. @EnableWebMvc
  3. @ComponentScan("org.springframework.samples.controllers")
  4. public class SpringMVCConfig extends WebMvcConfigurerAdapter {
  5. @Bean
  6. public ViewResolver viewResolver() {
  7. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  8. resolver.setPrefix("/WEB-INF/views/");
  9. resolver.setSuffix(".jsp");
  10. ;
  11. resolver.setExposeContextBeansAsAttributes(true);
  12. return resolver;
  13. }
  14.  
  15. @Override
  16. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  17. configurer.enable();
  18. }
  19. }

SpringMVCConfig

7, 能夠複製xml版本的jsp

8,根據SpringMVCConfig.java裏的componentscan裏的約定,咱們將HomeController放在controllers下面。內容同xml版本

9,測試