intellij+spring boot+spring MVC Create HelloWorld sample full steps

Source: Internet
Author: User
Tags spring initializr

1. Create Spring Boot Project

Select Spring Initializr, and then select default

Click Next to fill in the project information

Click "Next" and select Web->web

Click "Next" to complete the project information

Click "Finish" to open the new window after the project structure is as follows

2. Add a rest controller

Right-click on Com.spboot.mvcdemo to add new class

Create the Hellocontroller code as follows

Package Com.spboot.mvcdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import Org.springframework.web.bind.annotation.RestController;
@RestController
Public class Hellocontroller {
@RequestMapping ("/")
Public String Hello () {
return "Hello world!";
}
}

Then run directly, after running to access http://localhost:8080

In this we can develop the API through rest.

3. Add MVC support

Adding SPRINGMVC and Themeleaf dependencies to the POM

<dependency>
???? <groupId>org.springframework</groupId>
???? <artifactid>spring-webmvc</artifactid>
</Dependency>
<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactid>spring-boot-starter-thymeleaf</artifactid>

</Dependency>

Creating the Mvccontroller Class

The code is as follows:

Com.spboot.mvcdemo;

Org.springframework.stereotype.Controller;
org.springframework.web.bind.annotation.RequestMapping;

@Controller
Hellomvccontroller {

???? @RequestMapping ("/mvc")
String Hello () {
"Hello";
???? }

}

Create a hello.html file under Resources/templates

The code is as follows:

Remember: The HTML page must be added

After the configuration is complete, rerun and access the HTTP://LOCALHOST:8080/MVC with the following effect:

This mode can be used for newly created projects, or for view layers that were previously used as front-end HTML. But for some old projects, using the JSP as a view, how to transform it, continue to see below.

4. add support for JSP

Add a dependency to the JSP in the Pom and comment out the dependencies of the Themeleaf

<!-- for compiling jsp-->
<dependency>
???? <groupId>org.apache.tomcat.embed</groupId>
???? <artifactid>tomcat-embed-jasper</artifactid>
<!--<scope>provided</scope>-->
</Dependency>

<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactid>spring-boot-starter-test</artifactid>
???? <scope>test</scope>
</Dependency>
<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactid>spring-boot</artifactid>
???? <version>2.0.1.release</version>
</Dependency>

Add the WebApp directory with the following structure:

Add a View page under the JSP directory.

Add the following configuration in the Application.properties

/web-inf/jsp/
. jsp

Add a mapping to the controller

@RequestMapping ("/welcome")
String Welcome () {
"Welcome";
}

Add Servletcomponentscan annotations to the startup class and Inherit Springbootservletinitializer
?

Access Http://localhost:8080/welcome after re-running the app

5. post to GitHub

Create a warehouse named Springbootmvcdemo, after the creation is complete,

Find a directory locally, clone a bit first.

Https://github.com/lileihappy123/springbootMVCDemo.git

After completion, there will be a Springbootmvcdemo directory on the Local

Copy the code to the directory

Because I have a graphical interface here so simple, right-click Git commit->master, Popup submission interface, select all, enter comments, click "Commit & Push"

Wait for upload to complete

Then go to GitHub to view the uploaded code

Follow the public number "kick genius", more it posture is waiting for you

intellij+spring boot+spring MVC Create HelloWorld sample full steps

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.