javascript
Thymeleaf的Spring数据
介紹
今天,我將討論更具體的問題。 這次沒有設計模式或算法:-)。 我們并不總是從頭開始設計軟件組件。 通常,我們必須嘗試使現有軟件組件協同工作。
Spring Boot是Java世界上最好的免費軟件之一。 它解決了Spring的許多配置問題。 它非常靈活,功能強大。
Spring Data是Spring項目集合的一部分。 它提供了用于處理數據庫的高級工具。 其中最有用的是自動存儲庫。 一個類可以實現JpaRepository,并且大多數用于處理數據的方法將自動創建。
Thymeleaf是一個HTML模板引擎。 它可以使用Spring Boot的某些功能,例如模板中Spring bean的調用方法以及許多其他內容。 官方文檔有很棒的教程。
我使用了spring-boot-starter-parent版本2.0.1.RELEASE – 2.0.4.RELEASE。 其他依賴項由Spring Boot提供。
問題描述
任何與Spring Boot,Spring Data和Thymeleaf一起使用的應用程序的主要思想是編輯數據庫中的數據。 Spring-boot-starter-data-jpa包含Hibernate,可用于處理數據庫中的數據。 Thymeleaf可用于向用戶顯示數據。 Spring Boot將它們連接在一起。
一個非常簡單的場景包括一個與另一實體具有一對多關系的實體。 用戶希望能夠創建一個新實體并在HTML中選擇另一個實體
選擇框。
這是第一個問題出現的地方。 如果使用標準的Thymeleaf結構,則不能組裝后備bean。在選擇框中使用以下結構選擇的對象:
<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創建的。 我沒有在官方文檔中提及此內容。
解
經過一些調試后,我找到了根本原因。 原來Thymeleaf將所有字段作為參數傳遞給POST請求。 它使用toString方法將對象轉換為String并作為參數添加到POST請求中。 它發送這樣的參數:
room: Room+[id=273,+name=room111]在控制器方法中,必須將該值轉換回對象形式。 Spring Boot使用轉換器來做到這一點。
解決方案是–向conversionService注冊適當的轉換器。 并在實體的toString方法中使用這些轉換器,以確保使用相同的方法來轉換為String形式并返回。
下一個問題
聽起來很有趣不是嗎? 已經找到解決方案,但是還有更多問題嗎? 實際上,所描述的解決方案在沒有Spring Data的情況下效果很好。 使用Spring Data,轉換再次失敗。 而且即使沒有Spring Data也不需要該bean,Spring Boot仍希望您創建entityManagerFactory bean。
下一個解決方案
可以通過在Internet上進行一些深入的搜索來解決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;}第二個問題原來更加復雜,需要大量調試。 最終,我發現spring-data某種程度上改變了Spring Boot使用的轉換服務。 使用Spring Data mvcConversionService代替默認的conversionService。 格式程序/轉換器必須添加到WebMvcConfigurer類(實現WebMvcConfigurer的類)中。 方法是addFormatters:
@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new <SomeConverter>);... 現在,所有問題已解決,Spring Data可以與Thymeleaf一起使用。
快樂的編碼和勤奮的調試!
翻譯自: https://www.javacodegeeks.com/2018/11/spring-data-thymeleaf.html
總結
以上是生活随笔為你收集整理的Thymeleaf的Spring数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel分段求和分段序号技巧如何进行分
- 下一篇: nashorn预编译_Java 8:在新