springboot设置首页(通过方法映射)

版权声明:原创 https://blog.csdn.net/rambler_designer/article/details/88990170

为spring boot项目配置一个默认的访问页面(Home Page)

个人理解,spring boot访问不需要项目名,所有的项目都是通过方法映射控制的

访问项目一般通过http://localhost:8080

也就是默认请求是"/",所以我们只需要为"/"配置一个默认的相应方法即可

有个这个理论做基础,开始测试

需要的POM依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>    
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>

application.properties不需要配置,不过我为了此时方便修改了一下端口,这样直接访问localhost就是自己的项目

#server port
server.port=80

映射类及映射方法

package com.rambler.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {
    private static final String INDEX = "index";

    @RequestMapping("/")
    public String welcomePage(Model model){
        model.addAttribute("name","rambler_designer");
        return INDEX;
    }
}

html页面代码

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1 th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

效果

猜你喜欢

转载自blog.csdn.net/rambler_designer/article/details/88990170