09--SpringBoot之初阶整合篇(上)

这篇将结合引擎模板thymeleaf,mysql数据库jap,简单的jQuery和vue。来构建一个图片上传和展示的小案例
其中maven配置,及配置文件配置这里就不废话了,详见:
04–SpringBoot之模板引擎–thymeleaf
07–SpringBoot之数据库JPA(CRUD)

本篇以实现功能为主,样式由下篇讲述。

1.图片访问接口文件夹:toly1994.com.toly01.config.WebConfig
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //在F:/SpringBootFiles/Image/下如果有一张 Excalibar.jpg的图片,那么:
        //【1】访问:http://localhost:8080/imgs/Excalibar.jpg 可以访问到
        //【2】html 中 <img src="imgs/Excalibar.jpg">
        registry.addResourceHandler("/imgs/**").addResourceLocations("file:F:/SpringBootFiles/Image/");
    }
}
2.文件上传页:templates/submit.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>文件上传页</title>
</head>
<!DOCTYPE HTML>
<body>
<h1>添加名剑</h1>
<form th:action="@{/submit_form}" th:object="${sword}" method="post" enctype="multipart/form-data">
    <p>名剑名称: <input type="text" th:field="*{name}"/></p>
    <p>名剑简介:<br> <textarea rows="10" th:field="*{info}"></textarea></p>
    <p>名剑来源:
        <input type="text" th:field="*{origin}"/></p>
    <p>名剑图片: <input type="file" name="file"/></p>
    <p><input type="submit" value="提交"/> <input type="reset" value="重置"/></p>
</form>
</body>
</html>
3.控制器:跳转+上传+插入数据库:
@Controller //注意由于是RequestBody 所以这里将@RestController拆分出来了
public class ShowPhotoController {

    @GetMapping("/show")
    public String swordList(Model model) {
        model.addAttribute("sword", new Sword());
        return "SwordList";
    }

    @GetMapping("/add_form")
    public String greetingForm(Model model) {
        model.addAttribute("sword", new Sword());
        return "submit";
    }

    @Autowired
    private SwordRepository mSwordRepository;

    @PostMapping("/submit_form")
    public String greetingSubmit(@ModelAttribute Sword sword,
                                 @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();//获取名字
        String path = "F:/SpringBootFiles/Image";
        File dest = new File(path + "/" + fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            sword.setImgurl("http://localhost:8080/imgs/" + fileName);
            sword.setCreate_time(new Date());
            sword.setModify_time(new Date());
            mSwordRepository.save(sword);
            return "SwordList";
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return "上传失败!";
        }
    }
}
4.显示界面:templates/SwordList.html

这里简单的使用了jquery和vue两位前端大佬。
简单介绍一下:$.getJSON('http://localhost:8080/swords/findall', function (data)
是说data是访问http://localhost:8080/swords/findall返回的数据,
这个接口详见:08–SpringBoot之统一化json输出与自定义异常捕获
imgData: data.data是说把data.data给imgData变量,还记得data.data就是所有sword对象的json化字符串
v-for="(val, key, index) in imgData" :key="index"就是遍历val就是单个对象。
val.imgurl 是图片访问的url,我把图片上传到指定文件夹,并将url放在数据库中,
即第3小点的:sword.setImgurl("http://localhost:8080/imgs/" + fileName);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HelloThymeleaf</title>
    <link rel="stylesheet" href="../static/css/my.css">
    <script src="../static/js/jquery-3.3.1.min.js"></script>
    <script src="../static/js/vue2.5.16.min.js"></script>
</head>
<body>
<div id="root">
    <h1>名剑表</h1>
    <ul>
        <li v-for="(val, key, index) in imgData" :key="index">
            <a href=""><val.imgurl width="300"></a>
        </li>
    </ul>
</div>
<script>
    $.getJSON('http://localhost:8080/swords/findall', function (data) {
            new Vue({
                el: "#root",//与id是box的元素绑定
                data: {//数据
                    imgData: data.data
                }
            });
        }
    );
</script>
</body>
</html>

插入天生牙

插入两个来看看效果,这样我就可以通过数据库的改变决定前端页面的显示
发布到服务器上,也可以让任何人通过接口添加条目,就像给它演变的可能,让它”活了”。
数据便是它流动的血液、它的肉体。不然的话它只是一个静态的只能观看的玩偶而已。
下一篇将用css对页面装饰一下,给我打造的”生物”一件新衣。

显示界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot是企业级开发的整体整合解决方案,特别用于快速构建微服务应用,旨在用最简单的方式让开发人员适应各种开发场景。 SpringBoot全套视频分为上下两部;本视频《SpringBoot高级》属于下部,着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式(整合Zookeeper/dubbo,整合SpringCloud),SpringBoot应用监管。 学习本套视频需要掌握SpringBoot;对于其他技术,视频包含快速入门讲解; 课程目录: 00、尚硅谷_SpringBoot_源码、课件 1、尚硅谷-SpringBoot高级-缓存-JSR107简介 2、尚硅谷-SpringBoot高级-缓存-Spring缓存抽象简介 3、尚硅谷-SpringBoot高级-缓存-基本环境搭建 4、尚硅谷-SpringBoot高级-缓存-@Cacheable初体验 5、尚硅谷-SpringBoot高级-缓存-缓存工作原理&@Cacheable运行流程 6、尚硅谷-SpringBoot高级-缓存-@Cacheable其他属性 7、尚硅谷-SpringBoot高级-缓存-@CachePut 8、尚硅谷-SpringBoot高级-缓存-@CacheEvict 9、尚硅谷-SpringBoot高级-缓存-@Caching&@CacheConfig 10、尚硅谷-SpringBoot高级-缓存-搭建redis环境&测试 11、尚硅谷-SpringBoot高级-缓存-RedisTemplate&序列化机制 12、尚硅谷-SpringBoot高级-缓存-自定义CacheManager 13、尚硅谷-SpringBoot高级-消息-JMS&AMQP;简介 14、尚硅谷-SpringBoot高级-消息-RabbitMQ基本概念简介 15、尚硅谷-SpringBoot高级-消息-RabbitMQ运行机制 16、尚硅谷-SpringBoot高级-消息-RabbitMQ安装测试 17、尚硅谷-SpringBoot高级-消息-RabbitTemplate发送接受消息&序列化机制 18、尚硅谷-SpringBoot高级-消息-@RabbitListener&@EnableRabbit 19、尚硅谷-SpringBoot高级-消息-AmqpAdmin管理组件的使用 20、尚硅谷-SpringBoot高级-检索-Elasticsearch简介&安装 21、尚硅谷-SpringBoot高级-检索-Elasticsearch快速入门 22、尚硅谷-SpringBoot高级-检索-SpringBoot整合Jest操作ES 23、尚硅谷-SpringBoot高级-检索-整合SpringDataElasticsearch 24、尚硅谷-SpringBoot高级-任务-异步任务 25、尚硅谷-SpringBoot高级-任务-定时任务 26、尚硅谷-SpringBoot高级-任务-邮件任务 27、尚硅谷-SpringBoot高级-安全-测试环境搭建 28、尚硅谷-SpringBoot高级-安全-登录&认证&授权 29、尚硅谷-SpringBoot高级-安全-权限控制&注销 30、尚硅谷-SpringBoot高级-安全-记住我&定制登陆页 31、尚硅谷-SpringBoot高级-分布式-dubbo简介 32、尚硅谷-SpringBoot高级-分布式-docker安装zookeeper 33、尚硅谷-SpringBoot高级-分布式-SpringBoot、Dubbo、Zookeeper整合 34、尚硅谷-SpringBoot高级-分布式-SpringCloud-Eureka注册中心 35、尚硅谷-SpringBoot高级-分布式-服务注册 36、尚硅谷-SpringBoot高级-分布式-服务发现&消费 37、尚硅谷-SpringBoot高级-热部署-devtools开发热部署 38、尚硅谷-SpringBoot高级-监管-监管端点测试 39、尚硅谷-SpringBoot高级-监管-定制端点 40、尚硅谷-SpringBoot高级-监管-自定义HealthIndicator

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值