SpringBoot学习02--SpringBoot访问HTML页面

 

本篇博客在SpringBoot学习01--Hello world程序的基础上进行,实现SpringBoot访问HTML页面功能。

 

操作前先了解:

SpringBoot的Web相关文件存放在src/main/resources目录下。

一般情况下static目录用于存放静态文件,例如css、图片等文件,templates用于存放HTML文件,

如果resources目录下没有这两个目录,需要自行新建出来。

 

步骤:

1.添加依赖

修改pom.xml文件,在</dependencies>的前一行添加thymeleaf模板引擎依赖

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

修改后IDEA右下角提示Maven projects need to be imported,选择Enable Auto-Import自动下载更新的依赖。

2.配置aplication.yml

重命名application.properties为application.yml(这样改之后可以让配置更加简洁)

修改aplication.yml为如下内容:

spring:
  thymeleaf:
    prefix: classpath:/templates/

3.编写HTML文件

在src/main/resources/templates目录下新建一个html文件

输入文件名 index-->OK

编写index.html内容

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>springboot web</title>
</head>
<body>
<h1>Hello Spring Boot!!!</h1>
<p th:text="${hello}"></p>
<div>
    <p th:text="${say}"></p>
</div>
</body>
</html>

 

4.编写控制类

新建一个存放控制类的controller包

新建控制类HTMLController.java

编辑HTMLController.java内容如下:

package com.jipson.demo.controller;

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

import java.util.HashMap;

@Controller
public class HTMLController {
    @RequestMapping("/hello")
    public String helloHtml(HashMap<String, Object> map, Model model) {
        model.addAttribute("say","欢迎欢迎,热烈欢迎");
        map.put("hello", "欢迎进入HTML页面");
        return "index";
    }
}

5.启动项目

右键DemoApplication类-->Run 'DemoApplication'

6.浏览器验证

浏览器输入localhost:8080/hello,看到如下界面为成功:

 

完整工程代码:

完整代码下载   提取码:fdls

 

 

参考:https://www.jianshu.com/p/198497e08e00

 

 

完成! enjoy it!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Vue Router需要以下步骤: 1. 首先,在你的Spring Boot项目中创建一个文件夹来存放Vue相关的文件,比如可以创建一个名为"src/main/resources/static"的文件夹。 2. 在该文件夹下创建一个新的文件夹,比如可以叫做"vue",用来存放Vue的相关代码。 3. 在"vue"文件夹下创建一个新的文件,比如可以叫做"app.vue",用来编写Vue的根组件。 4. 在"app.vue"中引入Vue Router,并声明路由配置。你可以像下面这样进行引入和配置: ```html <template> <div> <router-view></router-view> </div> </template> <script> import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); const router = new VueRouter({ routes: [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About } // 其他路由配置 ] }); export default { router }; </script> ``` 5. 在同样的文件夹下创建其他需要的组件文件,比如可以创建一个名为"home.vue"和"about.vue"的文件,分别用来编写Home和About页面的组件。 6. 在Spring Boot的后端代码中配置路由,使其能够处理Vue Router定义的路由请求。在你的后端控制器中添加以下配置: ```java @Controller public class ViewController { @RequestMapping({"/", "/{path:^(?!static$).*$}"}) public String forward() { return "forward:/index.html"; } } ``` 这个配置会将所有非"static"开头的路由请求都转发到index.html文件,这样Vue Router就能够处理这些路由请求了。 7. 在Spring Boot的配置文件中添加以下配置,以确保静态资源能够正确加载: ```properties spring.resources.static-locations=classpath:/static/ ``` 8. 最后,使用npm或者yarn安装Vue和Vue Router的依赖包,并编译打包你的Vue代码。 完成以上步骤后,你就可以在Spring Boot中使用Vue Router了。当访问Spring Boot的路由时,会自动加载Vue Router定义的路由,并渲染对应的组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值