美文网首页
运行Springboot第一个项目

运行Springboot第一个项目

作者: 义无反顾00 | 来源:发表于2019-12-16 19:45 被阅读0次

https://spring.io/guides/gs/serving-web-content/
是Spring的API可以在上面看到开源的项目

项目目录

Java文件夹是储存Java文件,resources是储存所有的的静态文件和配置文件。其中templates是模板目录需要在CoummunityApplication同级目录中创建控制类的文件夹,这样子项目会自动运行控制类。application.properties为配置文件,可以在里面修改端口等等。

HelloController.java文件

package com.crow.coummunity.controller;

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


@Controller
public class HelloController {
    @GetMapping("/hello")
    //@RequestParam 中是接收请求参数
    public String hello(@RequestParam(name = "name")String name, Model model){
        //将接受到的name传入model中
        model.addAttribute("name",name);
        return "Hello";
    }
}

Hello.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>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

直接运行CoummunityApplication.java文件


运行成功

相关文章

网友评论

      本文标题:运行Springboot第一个项目

      本文链接:https://www.haomeiwen.com/subject/tmgdnctx.html