javascript
Thymeleaf的Spring数据
介紹
今天,我將討論更具體的問題。 這次沒有設(shè)計(jì)模式或算法:-)。 我們并不總是從頭開始設(shè)計(jì)軟件組件。 通常,我們必須嘗試使現(xiàn)有軟件組件協(xié)同工作。
Spring Boot是Java世界上最好的免費(fèi)軟件之一。 它解決了Spring的許多配置問題。 它非常靈活,功能強(qiáng)大。
Spring Data是Spring項(xiàng)目集合的一部分。 它提供了用于處理數(shù)據(jù)庫的高級工具。 其中最有用的是自動存儲庫。 一個(gè)類可以實(shí)現(xiàn)JpaRepository,并且大多數(shù)用于處理數(shù)據(jù)的方法將自動創(chuàng)建。
Thymeleaf是一個(gè)HTML模板引擎。 它可以使用Spring Boot的某些功能,例如模板中Spring bean的調(diào)用方法以及許多其他內(nèi)容。 官方文檔有很棒的教程。
我使用了spring-boot-starter-parent版本2.0.1.RELEASE – 2.0.4.RELEASE。 其他依賴項(xiàng)由Spring Boot提供。
問題描述
任何與Spring Boot,Spring Data和Thymeleaf一起使用的應(yīng)用程序的主要思想是編輯數(shù)據(jù)庫中的數(shù)據(jù)。 Spring-boot-starter-data-jpa包含Hibernate,可用于處理數(shù)據(jù)庫中的數(shù)據(jù)。 Thymeleaf可用于向用戶顯示數(shù)據(jù)。 Spring Boot將它們連接在一起。
一個(gè)非常簡單的場景包括一個(gè)與另一實(shí)體具有一對多關(guān)系的實(shí)體。 用戶希望能夠創(chuàng)建一個(gè)新實(shí)體并在HTML中選擇另一個(gè)實(shí)體
選擇框。
這是第一個(gè)問題出現(xiàn)的地方。 如果使用標(biāo)準(zhǔn)的Thymeleaf結(jié)構(gòu),則不能組裝后備bean。在選擇框中使用以下結(jié)構(gòu)選擇的對象:
<form action="#" th:action="@{/<some Action>}" th:object="${beanObj}" method="post">.... <other fields><select th:field="*{room}" class="textinput"><option th:each="currRoom : ${allRooms}" th:value="${currRoom}" th:text="${currRoom.name}">no name</option></select> </form>不是由Thymeleaf創(chuàng)建的。 我沒有在官方文檔中提及此內(nèi)容。
解
經(jīng)過一些調(diào)試后,我找到了根本原因。 原來Thymeleaf將所有字段作為參數(shù)傳遞給POST請求。 它使用toString方法將對象轉(zhuǎn)換為String并作為參數(shù)添加到POST請求中。 它發(fā)送這樣的參數(shù):
room: Room+[id=273,+name=room111]在控制器方法中,必須將該值轉(zhuǎn)換回對象形式。 Spring Boot使用轉(zhuǎn)換器來做到這一點(diǎn)。
解決方案是–向conversionService注冊適當(dāng)?shù)霓D(zhuǎn)換器。 并在實(shí)體的toString方法中使用這些轉(zhuǎn)換器,以確保使用相同的方法來轉(zhuǎn)換為String形式并返回。
下一個(gè)問題
聽起來很有趣不是嗎? 已經(jīng)找到解決方案,但是還有更多問題嗎? 實(shí)際上,所描述的解決方案在沒有Spring Data的情況下效果很好。 使用Spring Data,轉(zhuǎn)換再次失敗。 而且即使沒有Spring Data也不需要該bean,Spring Boot仍希望您創(chuàng)建entityManagerFactory bean。
下一個(gè)解決方案
可以通過在Internet上進(jìn)行一些深入的搜索來解決entityManagerFactory bean的問題。 這是我最終得到的解決方案:
@Primary@Beanpublic LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource ds) {LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();em.setDataSource(ds);em.setPackagesToScan("<some packages>");JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();em.setJpaVendorAdapter(vendorAdapter);em.setJpaProperties(additionalProperties());return em;}@Beanpublic SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {return emf.unwrap(SessionFactory.class);} private Properties additionalProperties() {Properties properties = new Properties();properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");properties.setProperty("hibernate.default_schema", "public");properties.setProperty("hibernate.show_sql", "true");// Validation will fail because the tables use bigint as the ID but it is mapped to the Integer type by Hibernate// Validation expects a 8-bit number as the mapping to bigint.properties.setProperty("hibernate.hbm2ddl.auto", "none");return properties;}第二個(gè)問題原來更加復(fù)雜,需要大量調(diào)試。 最終,我發(fā)現(xiàn)spring-data某種程度上改變了Spring Boot使用的轉(zhuǎn)換服務(wù)。 使用Spring Data mvcConversionService代替默認(rèn)的conversionService。 格式程序/轉(zhuǎn)換器必須添加到WebMvcConfigurer類(實(shí)現(xiàn)WebMvcConfigurer的類)中。 方法是addFormatters:
@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new <SomeConverter>);... 現(xiàn)在,所有問題已解決,Spring Data可以與Thymeleaf一起使用。
快樂的編碼和勤奮的調(diào)試!
翻譯自: https://www.javacodegeeks.com/2018/11/spring-data-thymeleaf.html
總結(jié)
以上是生活随笔為你收集整理的Thymeleaf的Spring数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel分段求和分段序号技巧如何进行分
- 下一篇: 电脑开不了机了电脑开不了机了,主板上亮红