Spring MVC Series Hello World (SpringBoot) (vi)

Foreword

We will SpringBoot can be seen as a master, he said the popular point is to simplify the configuration, let's talk about how to enable and use Spring MVC in the SpringBoot, Spring MVC and MVC in .NET or .NET Core thought the same, but noun not the same as nothing, well, let's see how to use Spring MVC in SpringBoot in.

Spring MVC之Hello World

 In SpringBoot our project created by default, we add the following packages pom.xml Maven Management Pack for JSP's compiled

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

It should be an additional note, the package spring-boot-starter-web While contain spring-boot-starter-tomcat, while spring-boot-starter-tomcat contains a tomcat-embed-core package, but the package tomcat-embed-core and does not contain tomcat-embed-jasper, in fact, is a tomcat-embed-core of tomcat-embed-jasper dependency, we can see by the above, the packet tomcat-embed-jasper labeled provided (provided) is, Thus we show that the container or provide desired JDK dependencies at runtime, this scope is only available on the classpath compiled and tested, and is not transmitted. In short, spring-boot-starter-web contains tomcat embedded dependencies, but does not include jasper embedded dependencies, so we need to explicitly declare to compile JSP. Next we create a file directory according to the agreement and configure Java Web search view that is the position of the JSP application.properties profile.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Next we create HelloWorld controller, as follows:

package com.demo.springboot.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorldController {

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public String helloWorld(@RequestParam String name, ModelMap model) {
        model.put("greeting", "Hello:" + name + " from Spring 4.3x MVC");
        return "welcome";
    }
}

@Controller annotation by the class labeled spring bean, it may be processed according to different HTTP request specified in the class mapping method or the respective controllers, and for @RequestMapping annotation maps the request to a specific Web handler class and / processing procedures or methods, annotated with several properties @RequestMapping [value, method, params, ..], may be used to map the range to a narrow selection more specifically, the method additionally with a declaration mapping attribute value, satisfying / type of request, the method greeting format attribute for specifying the method can serve HTTP request, if the map does not contain the "method" attribute mapping method, the controller will handle all types of requests on the URL of the map. Annotations @RequestParam parameters and for receiving a request from the query string parameter, a ModelMap a Map implementation, is to bind the value can be acquired so that the view, view helloWorld the return value, these values ​​have the suffix, and suffix and prefix defined in view parser as a prefix to form a view of the actual file name. Finally, we create welcome.jsp view files, as follows:

<%@ page contentType="text/html;" pageEncoding="utf-8" %>
<html>

<head>
    <title>Hello World</title>
</head>

<body>
 ${greeting}!
</body>

</html>

Note on this page to view the page encoding declaration, or else the background for the Chinese garbled, as shown in the question mark in the view:

to sum up 

In this section we started learning Java Web, and to view the final output Hello World ends, side dishes every day accumulation point, thank you for reading, we see the next section.

Guess you like

Origin www.cnblogs.com/CreateMyself/p/12158879.html