Spring Boot Learning (lesson three)

Source: Internet
Author: User

In this lesson, we talk about the use of controller:

Next, you'll demonstrate the use of templates:

Change the @restcontroller into a controller.

Then start the project and find the error.

In console:

Configure in Pom.xml:

Then add a directory to the resources named templates and create a new index.html in the templates:

The changes in Hellocontroller.java are as follows:

To access the browser:

This is actually equivalent to a JSP, except that the template engine uses Thymeleaf.

This approach is not recommended and will result in a lot of performance loss.

Use @controller and @responsebody instead of @restcontroller

To access the browser:

See here understand that actually @restcontroller this annotation is used to return the JSON data, without adding the words is to return the page.

New requirements Come: if we want to access through Hello also want to access through Hi, how to deal with it?

Change to:

We can also add @requestmapping to the entire class:

To access the browser:

If we want to change the method to post, this time directly through the browser is not directly accessible, you can use the Postman tool:

Then if the method is not written, both methods can be accessed, but not recommended, because the respective application scenarios, specific can see my next article.

Next, look at the @pathvariable:

will be as follows:

Change to:

To access the browser:

You can also write this:

To access the browser:

There is another way:

To access the browser:

There are several properties in the @RequestParam to understand:

To access the browser:

@RequestMapping can be simplified:

can also be used @postmapping () depending on the situation

Spring Boot Learning (lesson three)

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.