Spring Boot Learning Advanced Notes (i)-initial experience, creating basic Web Features

Source: Internet
Author: User

What is Spring boot? , what are the benefits of using spring boot? These things not too much statement, self-Baidu, in this as just a developer's point of view, record their own learning spring boot process, as a learning note to share with you, if there is anything not rigorous or wrong place, please leave a message to point out. First, initialize the structure of the project

Create a basic MAVEN structure project framework through the official website (http://start.spring.io/), such as! Click the button "Generation Project" to generate a basic directory, and then import to eclipse, you can write code! In the process of importing the project, select Maven-Existing maven Projects.

The overall project structure is as follows:

Ii. Introduction of Web Modules

1. In the MAVEN siege configuration file "Pom.xml", add

<dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId> </dependency>

There are two modules in the Pom.xml file by default:

Spring-boot-starter: core modules, including automatic configuration support, logging and YAML;

Spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito

2, write controller, in the package "controller" directory to create "Helloworldcontroller.java" class, through the class annotations, control the specific format of the returned data:

Package Com.zh.SpringBootDemo.controller;

Import Org.springframework.stereotype.Controller;

Import Org.springframework.ui.ModelMap;

Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;

@RestController //returns JSON data

@Controller //Jump to the corresponding page

Public class Helloworldcontroller {

@RequestMapping("/hello")

Public String Index () {

return "Hello World";

}

@RequestMapping ("/")

Public String Index (Modelmap map) {

//Add an attribute to read in the template

Map.addattribute ("host", "http://blog.didispace.com");

//Return template file name, corresponding to src/main/resources/templates/index.html

return "index";  

//    }

}

Third, run view

After running "Springbootdemoapplication.java" under the project root, open "http./Localhost:8080/hello" To see the returned results!

Spring Boot Learning Advanced Notes (i)-initial experience, creating basic Web Features

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.