, Gradle.
, :
Git: git clone https://github.com/spring-guides/gs-serving-web-content.git
cd gs-serve-web-content/initial
-.
, gs-serve-web-content/complete.
Gradle
script. , Spring, , Gradle.
; , mkdir -p src/main/java/hello on * nix systems:
└── src
└── main
└── java
└── hello
Gradle
Gradle.
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("junit:junit")
}
The Spring Boot gradle plugin provides many convenient features:
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the public static void main() method to flag as a runnable class.
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
**Create a web controller**
In Spring’s approach to building web sites, HTTP requests are handled by a controller. You can easily identify these requests by the @Controller annotation. In the following example, the GreetingController handles GET requests for /greeting by returning the name of a View, in this case, "greeting". A View is responsible for rendering the HTML content:
src/main/java/hello/GreetingController.java
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
, . .
@RequestMapping , HTTP-/ greeting().
GET vs. PUT, POST .., @RequestMapping HTTP . @RequestMapping (method = GET), .
@RequestParam String name greeting(). ; , "". name Model, .
, Thymeleaf, HTML-. Thymeleaf greeting.html th: text, ${name}, .
src/main/resources/templates/greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
-
- , . . , Spring Boot , spring -boot-devtools.
LiveReload
,
, WAR , , , . JAR , Java main() . Spring Tomcat HTTP .
src/main/java/hello/Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication - , :
@Configuration bean .
@EnableAutoConfiguration Spring "", beans , beans .
@EnableWebMvc Spring MVC, Spring Boot , spring -webmvc . , - , DispatcherServlet.
@ComponentScan Spring , hello, .
main() Spring Boots SpringApplication.run() . , XML? web.xml. - 100% - Java-, .
JAR
Gradle Maven. JAR , , , . , , ..
Gradle, , . /gradlew bootRun. JAR , . /gradlew build. JAR :
java -jar build/libs/gs-serve-web-content-0.1.0.jar
Maven, , . /mvnw spring -boot: run. JAR . /mvnw. JAR :
java -jar target/gs-serve-web-content-0.1.0.jar
runnable JAR. WAR.
. .
, - , http://localhost:8080/greeting, :
", !"
http://localhost:8080/greeting?name=User. , "Hello, World!". ", !":
", !"
, @RequestParam GreetingController , . "", .
, HTML JavaScript CSS, Spring Boot, . Spring Boot "/static" ( "/public" ). index.html , " ", , , , http://localhost:8080/ . :
src/main/resources/static/index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
and when you restart the app you will see the HTML at http://localhost:8080/.