web开发

简介: web开发

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

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

image.png

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,页面显示效果如下:

image.png

4. 自定义错误页面

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

image.png

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+的错误

相关文章
|
8月前
|
SQL 数据挖掘 关系型数据库
阿里云百炼|析言GBI全新发布:联合云上数据库,助力企业轻松实现ChatBI
析言GBI是阿里云推出的一款基于AI的智能数据分析产品,通过自然语言处理实现对话式数据分析。用户无需编写代码,即可轻松进行数据查询、分析和可视化。该产品支持多种数据库连接方式(如MySQL、PostgreSQL等),并提供多版本选择以适应不同业务需求。即将发布的动态规划BI分析功能将进一步提升复杂问题的拆解与综合分析能力。欢迎访问阿里云百炼应用广场体验析言GBI,并享受200次免费问题额度。
|
10月前
|
存储 Go PHP
Go语言中的加解密利器:go-crypto库全解析
在软件开发中,数据安全和隐私保护至关重要。`go-crypto` 是一个专为 Golang 设计的加密解密工具库,支持 AES 和 RSA 等加密算法,帮助开发者轻松实现数据的加密和解密,保障数据传输和存储的安全性。本文将详细介绍 `go-crypto` 的安装、特性及应用实例。
496 0
|
10月前
|
存储 监控 安全
什么是日志管理,如何进行日志管理?
日志管理是对IT系统生成的日志数据进行收集、存储、分析和处理的实践,对维护系统健康、确保安全及获取运营智能至关重要。本文介绍了日志管理的基本概念、常见挑战、工具的主要功能及选择解决方案的方法,强调了定义管理目标、日志收集与分析、警报和报告、持续改进等关键步骤,以及如何应对数据量大、安全问题、警报疲劳等挑战,最终实现日志数据的有效管理和利用。
1578 0
|
弹性计算 云计算 开发者
阿里云服务器租用价格表整理汇总,2024年阿里云价格表查询整理
在云计算服务市场上,阿里云凭借其卓越的性能和稳定的品质,赢得了广大用户的信赖。对于许多个人开发者和小型企业来说,选择阿里云的服务器往往是一个明智的选择。那么,阿里云服务器的租用价格是怎样的呢?今天我们就为大家带来最新的阿里云服务器租用价格表。
|
存储 算法 数据挖掘
向量数据库技术分享
向量数据库主要用于支持高效的向量检索场景(以图搜图、以文搜图等),通过本次培训可以掌握向量数据库的核心理论以及两种向量索引技术的特点、场景与算法原理,并通过实战案例掌握向量数据库的应用与性能优化策略。
1297 3
|
计算机视觉 开发者 Python
OpenCV中Fisherfaces人脸识别器识别人脸实战(附Python源码)
OpenCV中Fisherfaces人脸识别器识别人脸实战(附Python源码)
450 0
|
PHP
PHP微信公众号投票活动系统源码
PHP微信公众号投票活动系统源码
325 11
|
人工智能 BI
【动态规划】最长非降子序列 01背包 插入加号
1. 计算给定整数序列的最长非升子序列。 2. 解决 0-1 背包问题,找出使总价值最大的物品组合。 3. 找出在整数中插入加号的方法,使得加号后的整数和最小。
80 0
|
开发框架 缓存 前端开发
|
Linux Ubuntu

热门文章

最新文章