Simple Spring Boot MVC and springbootmvc
Recently I started to look at Spring Boot and found it is really convenient to develop it. Today, we will implement a simple Spring MVC request, pure Java code.
1. Maven is indispensable. First, let's take a look at the dependencies loaded:
<? Xml version = "1.0" encoding = "UTF-8"?> <Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion> 4.0.0 </modelVersion> <groupId> com. selrain </groupId> <artifactId> chapter-1 </artifactId> <version> 0.0.1-SNAPSHOT </version> <packaging> jar </packaging> <name> chapter-1 </name> <descriptio N> Demo project for Spring Boot </description> <parent> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 1.5.1.RELEASE </version> <relativePath/> <! -- Lookup parent from repository --> </parent> <properties> <project. build. sourceEncoding> UTF-8 </project. build. sourceEncoding> <project. reporting. outputEncoding> UTF-8 </project. reporting. outputEncoding> <java. version> 1.8 </java. version> </properties> <dependencies> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> // thymeleaf template, which will be used later <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-thymeleaf </artifactId> </dependency> <groupId> org. projectlombok </groupId> <artifactId> lombok </artifactId> <optional> true </optional> </dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-test </artifactId> <scope> test </scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> </project>
2. Controller
@Controllerpublic class GreetingController { @RequestMapping("/greeting") public String greeting(String name, Model m){ m.addAttribute("name",name); return "greeting"; }}
3. The page is stored in the src/main/resources/templates/directory for easy parsing.
<!DOCTYPE html>
4. Finally, the main program Portal :)
@SpringBootApplicationpublic class Chapter1Application { public static void main(String[] args) { SpringApplication.run(Chapter1Application.class, args); }}
Finally, start the project and enter the address http: // localhost: 8080/greeting in the browser? Name = world to see the effect.
Last. If you want to set the welcome page, on the src/main/resources/create index.html page, when you enter http: // localhost: 8080/, the program automatically jumps to the page.
Learning at the beginning and making progress together :)
Official website portal:
Https://spring.io/guides/gs/serving-web-content/