springboot 添加背景图片

本文介绍了如何在SpringBoot的HTML页面中正确设置背景图片并实现自适应显示。通过调整body样式,设置`background-repeat:no-repeat`,`background-size:100% 100%`和`background-attachment:fixed`,可以确保图片完整且适应页面大小。参考了两篇CSDN博客文章来解决显示问题。
摘要由CSDN通过智能技术生成

1.目录结构

在这里插入图片描述
1.图片不能放到templates中,自己试了显示不出来

2.背景设置

背景的方式
背景的路径和位置很重要,否则显示不出来
结果:在这里插入图片描述
图片没有自适应
我们在body中单独设置

<body style="	background-repeat:no-repeat ;
				background-size:100% 100%;
				background-attachment: fixed;">

实现效果:
在这里插入图片描述
完整显示

参考文章:https://blog.csdn.net/qq_36481052/article/details/79075214
参考文章:https://blog.csdn.net/qq_39985298/article/details/89521277?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164644173516780261939349%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164644173516780261939349&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-2-89521277.pc_search_result_cache&utm_term=springboot+html%E8%83%8C%E6%99%AF%E8%87%AA%E9%80%82%E5%BA%94&spm=1018.2226.3001.4187

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以使用Java的BufferedImage类来生成图片,Spring Boot中可以使用Thymeleaf模板引擎来渲染图片。具体步骤如下: 1. 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 创建一个Controller类,用于处理请求并生成图片: ```java @Controller public class ImageController { @GetMapping("/image") public void generateImage(HttpServletResponse response) throws IOException { // 创建一个BufferedImage对象 BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB); // 获取Graphics2D对象 Graphics2D g2d = image.createGraphics(); // 设置背景色为白色 g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, 200, 200); // 绘制一个矩形 g2d.setColor(Color.RED); g2d.drawRect(50, 50, 100, 100); // 释放资源 g2d.dispose(); // 将图片输出到浏览器 response.setContentType("image/png"); ImageIO.write(image, "png", response.getOutputStream()); } } ``` 3. 创建一个Thymeleaf模板,用于显示图片: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Image</title> </head> <body> <img th:src="@{/image}" alt="Image"> </body> </html> ``` 4. 启动应用程序并访问http://localhost:8080/,即可看到生成的图片。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值