上回

现在,我们已经能自行完成SpringBoot的初级项目搭建了,接下来看如何实现一些Web开发中的基础功能。

先看项目完整的目录结构:

1. 返回Json数据

创建model文件夹,并新建Person类,代码如下:

package com.example.hellospringboot.model;

public class Person {

    private int id = 0;

    private String name = "";

    public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

在controller文件夹下创建JsonController,代码如下:

package com.example.hellospringboot.controller;

import com.example.hellospringboot.model.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/json")
public class JsonController { @GetMapping("/person")
public Person person(){
Person person = new Person();
person.setId(1);
person.setName("祖斯特");
return person;
}
}

@RestController注解我们在上一节已经用过了,代表整个Controller请求方法仅返回纯数据,不包含Html页面信息

这种情况多见于前后端分离的情况,前端框架(如Vue)在拿到后端返回数据之后自行组织页面渲染

重启程序,访问地址 http://localhost:8080/json/person ,页面显示如下:

{"id":1,"name":"祖斯特"}

说明代码执行正确

2. 返回Html页面

接下来我们看如何返回完整的Html渲染页面

要实现这个功能,需要引入前端模板引擎,官方推荐Thymeleaf

我们在pom中加入其依赖配置:

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 引入thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

在controller文件夹下创建HtmlController类:

package com.example.hellospringboot.controller;

import com.example.hellospringboot.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/html")
public class HtmlController { @GetMapping("/person")
public ModelAndView person(){
ModelAndView mv = new ModelAndView();
Person person = new Person();
person.setId(1);
person.setName("祖斯特");
mv.addObject("person", person);
mv.setViewName("person");
return mv;
}
}

跟返回Json数据不同,HtmlController注解为@Controller,方法需要返回一个ModelAndView对象

mv.addObject 代表我们向前端Html模板提供绑定数据

mv.setViewName 代表我们要设定的Html模板,这里指定名称为:person

接下来我们在 resources/templates 路径下创建Thymeleaf模板文件 person.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Person测试页面</title>
</head>
<body>
<div>编号:<span th:text="${person.getId()}">默认编号</span></div>
<div>姓名:<span th:text="${person.getName()}">默认名字</span></div> </body>
</html>

Thymeleaf拥有优秀的设计理念,所有的模板文件即使没有后端程序也可以独立渲染(th标签不会引发异常),以供前端设计师查看效果

而 th:text="${xxx}" 代表程序执行时,标签的内容将动态替换为后端传过来的数据内容

重启程序,访问地址 http://localhost:8080/html/person ,页面显示如下:

编号:1
姓名:祖斯特

3. 静态资源访问

我们一般将静态文件(js、css、图片等)存放在单独的文件夹下,SpringBoot默认地址为 resources/static
但是为了使其能够正常访问,我们扔需要在application.properties中加入如下配置:
# 应用名称
spring.application.name=hellospringboot
# 应用服务 WEB 访问端口
server.port=8080 # 使用static作为静态资源根路径,且不需要其他路径前缀
spring.mvc.static-path-pattern=/**
spring.web.resources.static-locations=classpath:/static/

之后我们在static下放入一张图片head.png测试效果

person.html 加个<img>标签验证下效果:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Person测试页面</title>
</head>
<body>
<div>编号:<span th:text="${person.getId()}">默认编号</span></div>
<div>姓名:<span th:text="${person.getName()}">默认名字</span></div>
<div>
<img src="/head.png">
</div>
</body>
</html>

路径 src=/head.png 代表是static根路径

如果直接写 src=head.png 则为相对路径:static/html/head.png

需要注意这一点,大家可以自行尝试

访问地址 http://localhost:8080/html/person,页面显示效果如下:

4. 自定义错误页面

如果我们访问一个不存在的地址:http://localhost:8080/notexist,会弹出如下的错误页面:

SpringBoot已经为大家提供了自定义错误页面的方法,实现起来非常简单

我们在 resources/static 下创建文件夹 error,在error下创建 404.html 即可

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面不存在</title>
</head>
<body>
页面不存在!
</body>
</html>

重新启动程序,访问 http://localhost:8080/notexist ,效果如下:

页面不存在!

你可能感到困惑,这样岂不是要一个错误创建一个html文件?!

SpringBoot为我们提供了通配符支持,比如:4xx.html 可以代表401、402、403、404等所有400+的错误

以上。

关于 SpringBoot之基础Web开发 我们就介绍到这,下一节我们看如何实现SpringBoot和mysql数据库之间的交互,敬请期待。

Spring入门(二):SpringBoot之基础Web开发的更多相关文章

  1. 2.Python爬虫入门二之爬虫基础了解

    1.什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.想抓取什么?这个由你来控制它咯. ...

  2. Python爬虫入门二之爬虫基础了解

    1.什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.想抓取什么?这个由你来控制它咯. ...

  3. 转 Python爬虫入门二之爬虫基础了解

    静觅 » Python爬虫入门二之爬虫基础了解 2.浏览网页的过程 在用户浏览网页的过程中,我们可能会看到许多好看的图片,比如 http://image.baidu.com/ ,我们会看到几张的图片以 ...

  4. SpringBoot起飞系列-Web开发(四)

    一.前言 从今天你开始我们就开始进行我们的web开发,之前的一篇用SpringBoot起飞系列-使用idea搭建环境(二)已经说明了我们如何进行开发,当然这是搭建起步,接下来我们就开始进行详细的开发, ...

  5. springboot核心技术(三)-----web开发

    web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运 ...

  6. Spring Boot第五弹,WEB开发初了解~

    持续原创输出,点击上方蓝字关注我吧 目录 前言 Spring Boot 版本 前提条件(必须注意) 添加依赖 第一个接口开发 如何自定义tomcat的端口? 如何自定义项目路径? JSON格式化 日期 ...

  7. Spring+Maven+Dubbo+MyBatis+Linner+Handlebars—Web开发环境搭建

    本文主要分三部分,分别是:后台核心业务逻辑.桥梁辅助控制和前台显示页面. 本Web开发环境综合了多种工具,包括Maven包管理与编译工具.Dubbo分布式服务框架.MyBatis数据持久化工具.Lin ...

  8. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  9. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

随机推荐

  1. 《ASP.NET Core 6框架揭秘》样章[200页/5章]

    作为<ASP.NET Core 3 框架揭秘>的升级版,<ASP.NET Core 6框架揭秘>不仅针对ASP.NET Core 6的新特性进行了修订,并添加了若干原来没有的内 ...

  2. Grammarly for Chrome-语法、用词自动检查

    从语法和拼写到风格和语气,Grammarly帮助你消除写作错误,找到完美的词语来表达自己.当你在Gmail.Twitter.LinkedIn和几乎任何你发现自己在写作的地方写作时,你都会从Gramma ...

  3. private关键字的作用及使用和this关键字的作用

    封装的操作--private关键字 private的含义 1. private是一个权限修饰符,代表最小权限. 2. 可以修饰成员变量和成员方法. 3. 被private修饰后的成员变量和成员方法,只 ...

  4. identity server4 授权成功页面跳转时遇到错误:Exception: Correlation failed. Unknown location的解决方法

    一.异常信息描述 错误信息,看到这个页面是否耳熟能详担又不知道怎么解决 ,坑死个人不偿命,,,,,,,, 二.处理方法 1.在web项目中增加类SameSiteCookiesServiceCollec ...

  5. .net 温故知新:【6】Linq是什么

    1.什么是Linq 关于什么是Linq 我们先看看这段代码. List<int> list = new List<int> { 1, 1, 2, 2, 3, 3, 3, 5, ...

  6. NOI / 2.1基本算法之枚举2673:比赛排名

    总时间限制: 1000ms 内存限制: 65536kB 描述 5名运动员参加100米赛跑,各自对比赛结果进行了预测: A说:E是第1名. B说:我是第2名. C说:A肯定垫底. D说:C肯定拿不了第1 ...

  7. 前端(五)-Vue简单基础

    1. Vue概述 Vue (读音/vju/, 类似于view)是一套用于构建用户界面的渐进式框架,发布于2014年2月. 与其它大型框架不同的是,Vue被设计为可以自底向上逐层应用. Vue的核心库只 ...

  8. python subprocess相关操作

    python subprocess常用操作 1.subprocess模块的常用函数 函数 描述 subprocess.run() Python 3.5中新增的函数.执行指定的命令,等待命令执行完成后返 ...

  9. PLC转OPC UA的协议转换网关需要多少钱呢?

    嵌入式OPC UA网关BL102简化了OPC UA程序的开发与IIOT工业物联网应用 在制造业数字化升级过程中,我们碰到最多的工作便是针对每一款PLC去开发一套OPC UA程序,然后通过这套程序去读取 ...

  10. 运行 vue 项目时报错

    INFO Starting development server... ERROR Error: C - D:\T32890\Desktop\my-project\node_modules\@vue\ ...