Let me introduce Spring Boot MVC today and walk you through how to use Spring Boot to quickly build a simple web application.
**Environment Setup**
1. A text editor (like Vim, Emacs, or Sublime Text) or an IDE (such as Eclipse or IntelliJ IDEA).
2. Java environment (JDK 1.7 or higher).
3. Maven 3.0+ (built-in in Eclipse and IntelliJ; if using the command line, you’ll need to install it separately).
**Creating a Simple Web Application**
Spring Boot simplifies the development of web applications. To get started, include the `spring-boot-starter-web` dependency in your Maven project:
```xml
4.0.0
com.tianmaying
spring-web-demo
0.0.1-SNAPSHOT
jar
spring-web-demo
Demo project for Spring WebMvc
org.springframework.boot
spring-boot-starter-parent
1.2.5.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
```
Next, create `src/main/java/Application.java`:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String greeting() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
Run the application using `mvn spring-boot:run` or by running the `main()` method in your IDE. Then, open a browser and go to `http://localhost:8080`. You should see “Hello World!†displayed.
This simple code demonstrates how Spring Boot automatically sets up an embedded Tomcat server and handles HTTP requests. The `@SpringBootApplication` annotation enables auto-configuration and component scanning, while `@RestController` tells Spring that this class is a controller and that its return values should be written directly to the HTTP response body.
**URL Routing with @Controller**
Modern web apps often have multiple pages, each mapped to different URLs. You can define methods to handle specific routes using `@RequestMapping`. For example:
```java
@RestController
public class Application {
@RequestMapping("/")
public String index() {
return "Index Page";
}
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
```
You can also combine `@RequestMapping` on both the class and method levels:
```java
@RestController
@RequestMapping("/classPath")
public class Application {
@RequestMapping("/methodPath")
public String method() {
return "mapping url is /classPath/methodPath";
}
}
```
**Dynamic URL Parameters – @PathVariable**
To handle dynamic URLs like `/users/user1`, use `@PathVariable`:
```java
@RequestMapping("/users/{username}")
public String userProfile(@PathVariable("username") String username) {
return String.format("user %s", username);
}
@RequestMapping("/posts/{id}")
public String post(@PathVariable("id") int id) {
return String.format("post %d", id);
}
```
**HTTP Methods – GET, POST, etc.**
Different HTTP methods (GET, POST, etc.) can be specified using the `method` attribute:
```java
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginGet() {
return "Login Page";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginPost() {
return "Login Post Request";
}
```
**Template Rendering with Thymeleaf**
Instead of returning raw strings, you can render HTML templates. Change `@RestController` to `@Controller` and use a template engine like Thymeleaf:
```java
import org.springframework.ui.Model;
@Controller
public class HelloController {
@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
```
Add the following dependency to your `pom.xml`:
```xml
org.springframework.boot
spring-boot-starter-thymeleaf
```
Then create `src/main/resources/templates/hello.html`:
```html
Hello
```
**Serving Static Files**
Static files like CSS and JavaScript are automatically served from `src/main/resources/static`. Place your CSS and JS files there, and they will be accessible at paths like `/css/style.css` or `/js/main.js`.
With these tools, you can quickly build and deploy full-featured web applications using Spring Boot.
Shenzhen ChengRong Technology Co.,Ltd. , https://www.laptopstandsupplier.com