当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot thymeleaf 图片上传web项目根目录操作步骤

spring boot thymeleaf 图片上传web项目根目录操作步骤

2019年07月19日  | 移动技术网IT编程  | 我要评论

thymeleaf介绍

简单说, thymeleaf 是一个跟 velocity、freemarker 类似的模板引擎,它可以完全替代 jsp 。相较与其他的模板引擎,它有如下三个极吸引人的特点:

1.thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

2.thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现jstl、 ognl表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

3.thymeleaf 提供spring标准方言和一个与 springmvc 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

form方式上传:

//html:
<form enctype="multipart/form-data" method="post" action="/sell/imageupload">
<div class="modal-header">
 <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> </button>
 <h4 class="modal-title" id="mymodallabel">edit goods information</h4>
</div>
<div class="modal-body">
 <div class="input-group">
  <label class="col-lg-4">name:</label>
  <input class="col-lg-8" id="edit_name" value="${goods.name}" name="name"/>
 </div>
<div class="input-group">
  <label class="col-lg-4">code:</label>
  <input class="col-lg-8" id="edit_sn" name="sn" value="${goods.sn}" />
 </div>
<div class="input-group">
  <label class="col-lg-4">weight:</label>
  <input class="col-lg-8" id="edit_weight" name="weight" value="${goods.weight}" />
 </div>
<div class="input-group">
  <label class="col-lg-4">marketprice:</label>
  <input class="col-lg-8" id="edit_marketprice" name="marketprice" value="${goods.marketprice}" />
 </div>
<div class="input-group">
  <label class="col-lg-4">shopprice:</label>
  <input class="col-lg-8" id="edit_shopprice" name="shopprice" value="${goods.shopprice}" />
 </div>
<div class="input-group">
  <label class="col-lg-4">unit:</label>
  <input class="col-lg-8" id="edit_unit" name="unit" value="${goods.unit}" />
 </div>
<div class="input-group">
  <label class="col-lg-4">number:</label>
  <input class="col-lg-8" id="edit_number" name="number" value="${goods.number}" />
 </div>
 <div class="input-group">
  <label class="col-lg-4">detail:</label>
  <textarea class="col-lg-8" id="edit_detail" name="detail" value="${goods.detail}" />
 </div>
<div class="input-group">
 <!--<form enctype="multipart/form-data" method="post" action="/sell/imageupload">
  <input ype="hidden" id="edit_goods_sn" name="sn" value="${goods.sn}" />-->
  image<input type="file" id="edit_image" name="file"/>
  <input type="submit" value="upload"/>
 <!--</form>-->
 </div>
</div>
<div class="modal-footer">
 <button type="button" class="btn btn-default" data-dismiss="modal">close</button> 
 <input type="submit" class="btn btn-primary" id="edit_save" value="submit">提交更改</input>
</div>
</form>
//controller
 @requestmapping(value = "/save",method = requestmethod.post)
 public string savegoodspage(@requestparam(value = "id",required=false) string id,@requestparam(value = "name",required=false) string name,@requestparam(value = "sn",required=false) string sn,
        @requestparam(value = "number",required=false) string number,@requestparam(value = "weight",required=false) string weight,
        @requestparam(value = "marketprice",required=false) string marketprice,@requestparam(value = "shopprice",required=false) string shopprice,
        @requestparam(value = "unit",required=false) string unit, @requestparam(value = "detail",required=false) string detail,@requestparam (value="file")multipartfile file ) {
 if (!file.isempty()) {
  try {
   bufferedoutputstream out = new bufferedoutputstream(
     new fileoutputstream(new file("src/main/resources/static/images/product/" + sn + ".jpg")));//保存图片到目录下
   out.write(file.getbytes());
   out.flush();
   out.close();
   string filename = "\\/images\\/product\\/" + sn + ".jpg";
   /*user.settupian(filename);
   //userrepository.save(user);//增加用户*/
  } catch (filenotfoundexception e) {
   e.printstacktrace();
   return "upload error," + e.getmessage();
  } catch (ioexception e) {
   e.printstacktrace();
   return "upload error" + e.getmessage();
  }
 }
  //...其他操作
 }

补充:变量表达式和星号表达有什么区别吗?

如果不考虑上下文的情况下,两者没有区别;星号语法评估在选定对象上表达,而不是整个上下文什么是选定对象?就是父标签的值,如下:

 <div th:object="${session.user}">
 <p>name: <span th:text="*{firstname}">sebastian</span>.</p>
 <p>surname: <span th:text="*{lastname}">pepper</span>.</p>
 <p>nationality: <span th:text="*{nationality}">saturn</span>.</p>
 </div>

这是完全等价于:

 <div th:object="${session.user}">
  <p>name: <span th:text="${session.user.firstname}">sebastian</span>.</p>
  <p>surname: <span th:text="${session.user.lastname}">pepper</span>.</p>
  <p>nationality: <span th:text="${session.user.nationality}">saturn</span>.</p>
 </div>

当然,美元符号和星号语法可以混合使用:

 <div th:object="${session.user}">
  <p>name: <span th:text="*{firstname}">sebastian</span>.</p>
  <p>surname: <span th:text="${session.user.lastname}">pepper</span>.</p>
  <p>nationality: <span th:text="*{nationality}">saturn</span>.</p>
 </div>

总结

以上所述是小编给大家介绍的spring boot thymeleaf 图片上传web项目根目录操作步骤,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网