spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄操作步驟
thymeleaf介紹
簡(jiǎn)單說, Thymeleaf 是一個(gè)跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個(gè)極吸引人的特點(diǎn):
1.Thymeleaf 在有網(wǎng)絡(luò)和無網(wǎng)絡(luò)的環(huán)境下皆可運(yùn)行,即它可以讓美工在瀏覽器查看頁面的靜態(tài)效果,也可以讓程序員在服務(wù)器查看帶數(shù)據(jù)的動(dòng)態(tài)頁面效果。這是由于它支持 html 原型,然后在 html 標(biāo)簽里增加額外的屬性來達(dá)到模板+數(shù)據(jù)的展示方式。瀏覽器解釋 html 時(shí)會(huì)忽略未定義的標(biāo)簽屬性,所以 thymeleaf 的模板可以靜態(tài)地運(yùn)行;當(dāng)有數(shù)據(jù)返回到頁面時(shí),Thymeleaf 標(biāo)簽會(huì)動(dòng)態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動(dòng)態(tài)顯示。
2.Thymeleaf 開箱即用的特性。它提供標(biāo)準(zhǔn)和spring標(biāo)準(zhǔn)兩種方言,可以直接套用模板實(shí)現(xiàn)JSTL、 OGNL表達(dá)式效果,避免每天套模板、該jstl、改標(biāo)簽的困擾。同時(shí)開發(fā)人員也可以擴(kuò)展和創(chuàng)建自定義的方言。
3.Thymeleaf 提供spring標(biāo)準(zhǔn)方言和一個(gè)與 SpringMVC 完美集成的可選模塊,可以快速的實(shí)現(xiàn)表單綁定、屬性編輯器、國(guó)際化等功能。
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();
}
}
//...其他操作
}
補(bǔ)充:變量表達(dá)式和星號(hào)表達(dá)有什么區(qū)別嗎?
如果不考慮上下文的情況下,兩者沒有區(qū)別;星號(hào)語法評(píng)估在選定對(duì)象上表達(dá),而不是整個(gè)上下文什么是選定對(duì)象?就是父標(biāo)簽的值,如下:
<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>
這是完全等價(jià)于:
<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>
當(dāng)然,美元符號(hào)和星號(hào)語法可以混合使用:
<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>
總結(jié)
以上所述是小編給大家介紹的spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄操作步驟,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Springboot實(shí)現(xiàn)多數(shù)據(jù)源切換詳情
這篇文章主要介紹了Springboot實(shí)現(xiàn)多數(shù)據(jù)源切換詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-09-09
java.lang.ExceptionInInitializerError初始化程序中的異常錯(cuò)誤的解決
java.lang.ExceptionInInitializerError?異常在?Java?中表示一個(gè)錯(cuò)誤,該錯(cuò)誤發(fā)生在嘗試初始化一個(gè)類的靜態(tài)變量、靜態(tài)代碼塊或枚舉常量時(shí),本文就來介紹并解決一下,感興趣的可以了解一下2024-05-05
Spring?Boot請(qǐng)求處理之常用參數(shù)注解使用教程
這篇文章主要給大家介紹了關(guān)于Spring?Boot請(qǐng)求處理之常用參數(shù)注解使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
深入了解SparkSQL中數(shù)據(jù)的加載與保存
這篇文章主要為大家詳細(xì)介紹了SparkSQL中數(shù)據(jù)的加載與保存的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-11-11
Mybatis Plus條件構(gòu)造器ConditionConstructor用法實(shí)例解析
這篇文章主要介紹了Mybatis Plus條件構(gòu)造器ConditionConstructor用法實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場(chǎng)景
這篇文章主要為大家介紹了Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場(chǎng)景剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,2022-03-03
用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字
這篇文章主要介紹了用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字,需要的朋友可以參考下2017-02-02

