栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot向静态页面写入数据,实现动态改变页面

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot向静态页面写入数据,实现动态改变页面

在有模板的情况下不使用模板,可以使用重定向来返回页面。
文件写入需要先将字符串转换成byte[],转换方法content.getBytes(StandardCharsets.UTF_8)。
在程序执行结束的末尾一定要将输出流关闭。

@GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id){

        final GsWeather gsWeather = gsWeatherService.selectGsWeatherById(id);
        String content = "n" +
                "n" +
                "n" +
                "n" +
                "    n" +
                "    n" +
                "        input{n" +
                "            width:100%;n" +
                "            border:none;n" +
                "            text-align: center;n" +
                "            outline-color: aqua;n" +
                "        }n" +
                "        span{n" +
                "            display: inline-block;n" +
                "            width:25%;n" +
                "            text-align: center;n" +
                "        }n" +
                "        #table{n" +
                "            width:80%;n" +
                "            margin: 10px auto;n" +
                "        }n" +
                "    n" +
                "n" +
                "n" +
                "n" +
                ""+gsWeather.getHtml()+
                "n" +
                "n" +
                "n" +
                "";
        //获取文件路径,解决部署时路径问题
        ClassPathResource resource = new ClassPathResource("detail.html");
        String path = resource.getPath();
        //获取文件
        File file = new File(path);
        //写入文件
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            //文件写入需要先将字符串转换成byte[]
            //转换方法content.getBytes(StandardCharsets.UTF_8)
            byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
            fileOutputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //在有模板的情况下不使用模板,可以使用重定向来返回页面
        return "redirect:/html/detail.html";
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658245.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号