Simple Spring Boot MVC and springbootmvc

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.