springbootthymeleaf图⽚上传web项⽬根⽬录操作步骤
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>
spring mvc和boot区别<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.Bytes());
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项⽬根⽬录操作步骤,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论