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,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

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/

时间: 2025-01-17 01:31:34

Gradle一分钟实现Spring-MVC的相关文章

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring boot如何快速构建一个. Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务.支持约定大于配置,目的是尽可能快地构建和运行Spring应用. 之前我们创建基于Spring的项目需要考虑添加哪些Spring依赖和第三方的依赖.使用Spring Boot后,我们可

Spring学习(二)——使用用Gradle构建一个简单的Spring MVC Web应用程序

1.新建一个Gradle工程(Project) 在新建工程窗口的左侧中选择 [Gradle],右侧保持默认选择,点击next,模块命名为VelocityDemo. 2.在该工程下新建一个 module,在弹出的窗口的左侧中选择 [Gradle],右侧勾选[Spring MVC],如下图所示: 并勾选[Application server],下方选择框中选择Tomcat7.0,如无该选项,则选中右边的 [ New... ] -- [ Tomcat Server ], 配置 Tomcat .配置好后

2分钟在eclipse下使用SpringBoot搭建Spring MVC的WEB项目

转自:http://www.cnblogs.com/chry/p/5876752.html 1. 首先用eclipse创建一个maven工程, 普通maven工程即可 2. 修改pom如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.

利用maven构建一个spring mvc的helloworld实例

刚开始学习maven和spring mvc,学的云里雾里的 这里提供一个hello world实例,记录自己的学习之路 首先看maven官网的介绍 Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and do

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

spring MVC 管理HttpClient---实现在java中直接向Controller发送请求

在spring MVC中,大多数时候是由客户端的页面通过ajax等方式向controller发送请求,但有时候需要在java代码中直接向controller发送请求,这时可以使用HttpCilent实现. 首先用到的包是httpclient-4.3.5.jar和httpcore-4.3.2.jar 先看下面代码: package module.system.common; import java.io.IOException; import java.util.ArrayList; import

springboot Serving Web Content with Spring MVC

Serving Web Content with Spring MVC This guide walks you through the process of creating a "hello world" web site with Spring. What you'll build You'll build an application that has a static home page, and also will accept HTTP GET requests at:

第一个使用Spring Tool Suite(STS)和Maven建立的Spring mvc 项目

一.目标 在这篇文章中.我将要向您展示怎样使用Spring Frameworks 和 Maven build创建您的第一个J2ee 应用程序. 二.信息 Maven是一个java项目的构建工具(或者自己主动构建工具).它与Ant或Gradle非常想.Maven能够自己主动下载您项目中依赖的组件. 三.要求 1.应用于Java EE 的Spring Tool Suite(STS) (http://spring.io/tools/sts/all). 请选择与您的操作系统相应的安装文件. 我比較喜欢下

Spring MVC学习笔记(一)--------准备篇

这一系列笔记将带你一步一步的进入Spring MVC,高手勿喷. 首先你得安装以下的工具: JDK,虽然JDK8已经发布了一段时间了,但是由于我们并不会使用到里面的新特性,所以JDK6以上版本皆可以(需加入到PATH环境变量中): Servlet Container,为了能运行WEB应用程序,因此需要一个Web Container,这里我们建议Tomcat即可: IDE,一个好的IDE不仅能提高你开发的效率,还能降低你学习的成本,我们选择的是IntelliJ: 构建工具,推荐使用Gradle,它