Introduction to thymeleaf
Simply put, Thymeleaf is a template engine similar to Velocity and FreeMarker, which can completely replace JSP. Compared with other template engines, it has the following three extremely attractive features:
1.Thymeleaf can be run in an environment with or without a network, that is, it allows artists to view the static effects of pages in the browser, and also allows programmers to view the dynamic page effects with data on the server. This is because it supports html prototypes, and then adds additional attributes to the html tag to achieve the display method of templates + data. When the browser interprets html, undefined tag attributes will be ignored, so the template of thymeleaf can be run statically; when data is returned to the page, the Thymeleaf tag will dynamically replace the static content, making the page display dynamically.
2. Thymeleaf is out of the box. It provides two dialects: Standard and Spring Standard. You can directly apply templates to achieve JSTL and OGNL expression effects, avoiding the trouble of putting templates, jstl, and changing tags every day. At the same time, developers can also expand and create custom dialects.
3.Thymeleaf provides spring standard dialect and an optional module that is perfectly integrated with SpringMVC, which can quickly implement form binding, attribute editor, internationalization and other functions.
Upload form method:
//html: <form enctype="multipart/form-data" method="post" action="/sell/imageUpload"><div> <button type="button" data-dismiss="modal" aria-hidden="true"> </button> <h4 id="myModalLabel">Edit goods information</h4></div><div> <div> <label>name:</label> <input id="edit_name" value="${goods.name}" name="name"/> </div><div> <label>code:</label> <input id="edit_sn" name="sn" value="${goods.sn}" /> </div><div> <label>weight:</label> <input id="edit_weight" name="weight" value="${goods.weight}" /> </div><div> <label>marketPrice:</label> <input id="edit_marketPrice" name="marketPrice" value="${goods.marketPrice}" /> </div><div> <label>shopPrice:</label> <input id="edit_shopPrice" name="shopPrice" value="${goods.shopPrice}" /> </div><div> <label>unit:</label> <input id="edit_unit" name="unit" value="${goods.unit}" /> </div><div> <label>number:</label> <input id="edit_number" name="number" value="${goods.number}" /> </div><div> <!--<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> <button type="button" data-dismiss="modal">close</button> <input type="submit" id="edit_save" value="submit">Submit changes</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")));//Save the image to the directory out.write(file.getBytes()); out.flush(); out.close(); String filename = "///images///product///" + sn + ".jpg"; /*user.setTupian(filename); //userRepository.save(user);//add user*/ } catch (FileNotFoundException e) { e.printStackTrace(); return "upload error," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "upload error" + e.getMessage(); } } //...Other operations}Supplement: Is there any difference between variable expressions and asterisk expressions?
There is no difference between the two without considering the context; asterisk syntax evaluation is expressed on the selected object, not the entire context What is the selected object? It is the value of the parent tag, as follows:
<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>This is exactly equivalent to:
<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>Of course, the dollar sign and asterisk syntax can be mixed:
<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>Summarize
The above is the operation steps for uploading the spring boot thymeleaf image to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!