简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
form方式上传:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
//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();
}
}
//...其他操作
}
|
补充:变量表达式和星号表达有什么区别吗?
如果不考虑上下文的情况下,两者没有区别;星号语法评估在选定对象上表达,而不是整个上下文什么是选定对象?就是父标签的值,如下:
1
2
3
4
5
|
<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>
|
这是完全等价于:
1
2
3
4
5
|
<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>
|
当然,美元符号和星号语法可以混合使用:
1
2
3
4
5
|
<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项目根目录操作步骤,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/fengcai0123/article/details/79693562