Gradle一分钟实现Spring-MVC

前提:

1,已安装JDK

2, 有Intellij IDEA

3, 已安装Gradle

一分钟实现步骤:

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

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'Administrator' at '16-10-2 下午8:42' with Gradle 3.1
 *
 * This generated file contains a commented-out sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.1/userguide/tutorial_java_projects.html
 */
 
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:+'
    }
}

ext {
    springVersion = '4.3.3.RELEASE'
} 

//apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
apply plugin: 'org.akhikhl.gretty'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'idea'


// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
    maven { url 'http://repo.spring.io/release' } 
}

// In this section you declare the dependencies for your production and test code
dependencies {
    //core spring  
    compile ("org.springframework:spring-context:$springVersion")  
    compile ("org.springframework:spring-core:$springVersion")  
    compile ("org.springframework:spring-webmvc:$springVersion")  

    // The production code uses the SLF4J logging API at compile time
    compile ("org.slf4j:slf4j-api:1.7.21")

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile ("junit:junit:4.12")
}
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文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
web.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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="org.springframework.samples"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
spring-mvc-servlet.xml

关于Spring MVC的配置文件,最简的配置是申明p,context,mvc命名空间即可。上面p其实是没有用到的。

7,根据第6步的spring-mvc-servlet.xml的配置,还需要在WEB-INF文件夹下面创建一个views文件夹,并且新建文件home.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/10/2 0002
  Time: 21:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
     <title>Home</title>
 </head>
<body>
<h1>
     Hello world!
 </h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>
home.jsp

8,HomeController.java

package org.springframework.samples;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/10/2 0002.
 */
@Controller
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * .     * Simply selects the home view to render by returning its name.
     * .
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);

        return "home";
    }

}
HomeController.java

9,

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+的服务器才行,实现只需扩展一下

AbstractAnnotationConfigDispatcherServletInitializer
public class SampleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{SpringMVCConfig.class};
    }

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

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

@Configuration
@EnableWebMvc
@ComponentScan("org.springframework.samples.controllers")
public class SpringMVCConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        ;
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
SpringMVCConfig

7, 可以复制xml版本的jsp

8,根据SpringMVCConfig.java里的componentscan里的约定,我们将HomeController放在controllers下面。内容同xml版本

9,测试

转载于:https://www.cnblogs.com/SLKnate/p/spring-mvc-in-oneminitue-with-gradle.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值