# Note For Thymeleaf ###### tags: `Shannon`,`SpringBoot` 資料來源:https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#checkbox-fields 補充資料:https://www.itread01.com/content/1547106606.html 30天學習Spring: https://ithelp.ithome.com.tw/users/20107812/ironman/1538 # th:object用法 before ```typescript<h2> <p>Name: <span th:text="${user.name}">Jack</span>.</p> <p>Age: <span th:text="${user.age}">21</span>.</p> <p>friend: <span th:text="${user.friend.name}">Rose</span>.</p> </h2> ``` after ```typescript <h2 th:object="${user}"> <p>Name: <span th:text="*{name}">Jack</span>.</p> <p>Age: <span th:text="*{age}">21</span>.</p> <p>friend: <span th:text="*{friend.name}">Rose</span>.</p> </h2> ``` # 02 關於ResponseEntity * Download File時會使用到 * 可以透過ResponseEntity設置http response 內容、狀態與頭訊息 ## 2.1 ResponseEntity * ResponseEntity標示了整個http response 像是狀態碼、頭部訊息以及相應體內容。可以透過這個來對http response實現完整配置 ## 03 Thymeleaf使用@Component裡面的方法 * 如果想要使用某個Service方法,那初始化會有問題,因此如果要使用方法就要選能夠不用初始化的Service ```typescript <button type="button" class="btn btn-success" onclick="showCredentialModal(this.getAttribute('data-credentialId'), this.getAttribute('data-url'), this.getAttribute('data-userName'), this.getAttribute('data-password'))" th:data-credentialId="${cred.credentialId}" th:data-url="${cred.url}" th:data-userName="${cred.userName}" th:data-password="${@EncryptionService.decryptValue(cred.password, cred.key)}" >Edit</button> ``` > 不用初始化 ```typescript @Service public class EncryptionService { private Logger logger = LoggerFactory.getLogger(EncryptionService.class); public String decryptValue(String data, String key){ byte[] decryptedValue = null; try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); decryptedValue = cipher.doFinal(Base64.getDecoder().decode(data)); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { logger.error("這裡是Decrypt 發生錯誤喔" + e.getMessage()); } return new String(decryptedValue); } } ``` > 要初始化 ```typescript @Service public class CredentialService { private CredentialMapper credentialMapper; private EncryptionService encryptionService; public CredentialService(CredentialMapper credentialMapper,EncryptionService encryptionService){ this.credentialMapper = credentialMapper; this.encryptionService = encryptionService; } public String Decrypt(String password, String key){ return encryptionService.decryptValue(password, key); } } ```