前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot-09-之初阶整合篇(上)

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

作者头像
张风捷特烈
发布2018-09-26 17:38:27
4030
发布2018-09-26 17:38:27
举报

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

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

1.图片访问接口文件夹:toly1994.com.toly01.config.WebConfig
代码语言:javascript
复制
@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
代码语言:javascript
复制
<!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.控制器:跳转+上传+插入数据库:
代码语言:javascript
复制
@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);

代码语言:javascript
复制
<!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>

插入Excalibur

插入天生牙

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

显示界面

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.图片访问接口文件夹:toly1994.com.toly01.config.WebConfig
  • 2.文件上传页:templates/submit.html
  • 3.控制器:跳转+上传+插入数据库:
  • 4.显示界面:templates/SwordList.html
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档