Springboot访问resource下html静态页面并简单搭建

Springboot如何访问resource下html静态页面

1. 先把我的项目结构包发出来看

用的idea创建的springboot模板

2. 首先写好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>

这个html是顺手在spring官网复制的,用的thymeleaf模板先引入,所以需要导入依赖。

3. pom.xml中导入thymeleaf依赖

spring-boot-starter-test还有spring-boot-starter-web是我们创建springboot模板的时候自动加的依赖,
这时候我们应该在里面跟一段依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

4. 在application.properties为文件中配置文件路径

spring.thymeleaf.prefix=classpath:/templates/

这一行是标明模板地址的前缀,找页面的时候会先加载templates下的子目录
写上这么一行后就可以进行访问了

5. Controller访问路径

package com.example.boke.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(@RequestParam(name="name") String name,Model model){
        model.addAttribute("name",name);
        return "hello";
    }
}

_我的映射路径是_http://localhost:8087/helloname=%E9%99%88%E5%B7%A7%E5%87%A4
因为我用了注解传name的值所以才写了后面的名字,进来找bug的人如没写传值,此地可省略,然后我成功的页面

成功后的页面最后总结,遇见bug不要慌张,按步骤先检查代码,再检查环境,最后度娘。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值