Advertisement
haithienht

[Java EE] Maven + Spring + ThymeLeaf approach in NetBeans

Dec 5th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. Netbean: Java EE + Maven + Spring + ThymeLeaf approach
  2. 1. Tạo prj Maven Java EE
  3. 2. Ở ejb:
  4. + dùng persistence tạo entity (Entity Class from database) > ở bước cuối bỏ chọn tùy chọn Use column name..., Collection chọn List cho quen thuộc.
  5. + (Optional) Ở những entity có id thuộc dạng Identity thì xóa annotation @Notnull và thêm vào @GeneratedValue (strategy = GenerationType.IDENTITY) (mai mốt nhớ mỗi chữ generatedvalue rồi Ctrl+Space ra hết :)) )
  6. + new > Enterprise JavaBean > Session Beans For Entity Classes > chọn bảng > tick chọn local và nhập tên package
  7. + Clean build
  8.  
  9. 3. Ở web:
  10. + Add Spring framework
  11. + vào pom.xml kiếm "bom" và thêm thuộc tính sau vào cuối dependency đó
  12. <type>pom</type>
  13.  
  14. + Cũng trong pom.xml thêm mấy dependency sau
  15. <!-- Thymeleaf core -->
  16. <dependency>
  17. <groupId>org.thymeleaf</groupId>
  18. <artifactId>thymeleaf</artifactId>
  19. <version>3.0.11.RELEASE</version>
  20. </dependency>
  21. <!-- Thymeleaf spring 4 integration -->
  22. <dependency>
  23. <groupId>org.thymeleaf</groupId>
  24. <artifactId>thymeleaf-spring4</artifactId>
  25. <version>3.0.11.RELEASE</version>
  26. </dependency>
  27. <!-- Làm layout đơn giản hơn -->
  28. <dependency>
  29. <groupId>nz.net.ultraq.thymeleaf</groupId>
  30. <artifactId>thymeleaf-layout-dialect</artifactId>
  31. <version>2.4.1</version>
  32. </dependency>
  33.  
  34. + Xong rồi build with dependencies
  35.  
  36. + Để sử dụng được cả spring và thymeleaf thì config như sau:
  37. == trong applicationContext.xml, thêm đoạn sau:
  38. <bean id="templateResolver"
  39. class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
  40. <property name="prefix" value="/" />
  41. <property name="suffix" value=".html" />
  42. <property name="cacheable" value="true" />
  43. </bean>
  44.  
  45. <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
  46. <property name="templateEngine" ref="templateEngine" />
  47. </bean>
  48.  
  49. <bean id="templateEngine"
  50. class="org.thymeleaf.spring4.SpringTemplateEngine">
  51. <property name="templateResolver" ref="templateResolver" />
  52. <property name="enableSpringELCompiler" value="true" />
  53. <property name="additionalDialects">
  54. <set>
  55. <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
  56. </set>
  57. </property>
  58. </bean>
  59.  
  60. == bên dispatcher-servlet.xml thì như xài Spring bình thường
  61. <beans xmlns="http://www.springframework.org/schema/beans"
  62. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  63. xmlns:p="http://www.springframework.org/schema/p"
  64. xmlns:aop="http://www.springframework.org/schema/aop"
  65. xmlns:tx="http://www.springframework.org/schema/tx"
  66. xmlns:context="http://www.springframework.org/schema/context"
  67. xmlns:mvc="http://www.springframework.org/schema/mvc"
  68. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  69. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  70. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  71. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  72. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  73.  
  74. <context:annotation-config />
  75.  
  76. <!-- Chỉnh lại package ở đây để cho nó tìm controller -->
  77. <context:component-scan base-package="vn.aptech" />
  78. <!-- Custom đường dẫn cho resource -->
  79. <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
  80. <mvc:resources mapping="/admin_resources/**" location="/admin/resources/" cache-period="31556926"/>
  81.  
  82. <mvc:annotation-driven />
  83. </beans>
  84. == ở web.xml, tìm chỉnh mấy dòng sau
  85. <servlet-mapping>
  86. <servlet-name>dispatcher</servlet-name>
  87. <url-pattern>/</url-pattern>
  88. </servlet-mapping>
  89. <welcome-file-list>
  90. <welcome-file>/</welcome-file>
  91. </welcome-file-list>
  92.  
  93. + Để xài EJB bên prj ejb thì config tiếp
  94. == ở pom.xml, thêm dependency
  95. <dependency>
  96. <groupId>com.npht</groupId> <!-- Chỉnh package lại ở đây -->
  97. <artifactId>SpringThymeLeaf-ejb</artifactId> <!-- Chỉnh tên project ejb ở đây -->
  98. <version>1.0</version> <!-- Version cũng bắt buộc trùng với project ejb -->
  99. <scope>provided</scope>
  100. </dependency>
  101. == ở web.xml
  102. <ejb-local-ref>
  103. <ejb-ref-name>UsersFacade</ejb-ref-name> <!-- Tên để chút nữa khai báo mappedName bên controller -->
  104. <ejb-ref-type>Session</ejb-ref-type>
  105. <local>com.npht.sb.UsersFacadeLocal</local> <!-- Đường dẫn package đến class local của nó -->
  106. </ejb-local-ref>
  107.  
  108. + Xong rồi build with dependencies để đưa ejb vào
  109.  
  110. + Tạo Java Class tên IndexController đặt vào package ...controller (vn.aptech.controller), nội dung như sau
  111. @Controller
  112. // Map controller này là gốc cho đường dẫn: "(Tên prj web)/"
  113. @RequestMapping("")
  114. public class IndexController {
  115. // mappedName là cái ejb-ref-name khai báo ở web.xml
  116. @EJB(mappedName = "UsersFacade")
  117. UsersFacadeLocal usersFacade;
  118.  
  119. // Map cho đường dẫn: "(đường dẫn gốc controller)/"
  120. @RequestMapping(value = "", method = RequestMethod.GET)
  121. public String getHome(ModelMap mm) {
  122. mm.addAttribute("users", usersFacade.findAll());
  123. return "index";
  124. }
  125. }
  126. + Tạo trang layout.html ở ngoài Web Pages
  127. <!-- Khai báo thư viện thymeleaf ở thẻ gốc -->
  128. <html xmlns="http://www.w3.org/1999/xhtml"
  129. xmlns:th="http://www.thymeleaf.org"
  130. xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
  131.  
  132. <head>
  133. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  134. <!-- Title này sẽ được thay thế nếu như file chính có tag title có nội dung -->
  135. <title layout:fragment="title">Layout title</title>
  136. <!-- th:href, th:src,... dùng giá trị nằm trong @{/đường_dẫn_đi_từ_gốc} -->
  137. <!-- Như ở dưới thì đường dẫn được render ra là (tên project)/resources/index.css (nhớ tạo file index.css này để test) -->
  138. <link rel="stylesheet" th:href="@{/resources/index.css}"/>
  139. </head>
  140. <body>
  141. <!-- Khai báo fragment tên header, nếu như ở file chính có 1 cái header có fragment tên header thì cái này sẽ được thay thế bằng cái bên file chính -->
  142. <header layout:fragment="header">
  143. This is layout header
  144. </header>
  145.  
  146. <!-- Cũng tương tự như phần header -->
  147. <div layout:fragment="content">
  148. This is layout content
  149. </div>
  150.  
  151. <footer layout:fragment="footer">
  152. This is layout footer
  153. </footer>
  154. </body>
  155. </html>
  156.  
  157. + Tạo trang index.html ở ngoài Web Pages
  158. <!DOCTYPE html>
  159. <!-- Khai báo thư viện thymeleaf và khai báo template bằng layout:decorate với đường dẫn nằm trong ~{đường_dẫn_đi_từ_gốc_đến_tên_file_bỏ_đuôi_đi}-->
  160. <html xmlns="http://www.w3.org/1999/xhtml"
  161. xmlns:th="http://www.thymeleaf.org"
  162. xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  163. layout:decorate="~{layout}">
  164. <head>
  165. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  166. <!-- thay đổi title -->
  167. <title>Index header</title>
  168. </head>
  169. <body>
  170. <!-- thay đổi div có fragment content -->
  171. <div layout:fragment="content">
  172. <table border="1">
  173. <thead>
  174. <tr>
  175. <td>Username</td>
  176. <td>Name</td>
  177. <td>Birthday</td>
  178. <td>Gender</td>
  179. <td>Role</td>
  180. <td>Action</td>
  181. </tr>
  182. </thead>
  183. <tbody>
  184. <tr th:if="${users eq null or users.empty}">
  185. <td colspan="6">No users</td>
  186. </tr>
  187. <tr th:each="u : ${users}">
  188. <td th:text="${u.username}"></td>
  189. <td th:text="${u.name}"></td>
  190. <td th:text="${#calendars.format(u.birthday, 'dd/MM/yyyy')}"></td>
  191. <td th:text="${u.isMale} == true?'Male':'Female'"></td>
  192. <td th:text="${u.isAdmin} == true?'Admin':'User'"></td>
  193. </tr>
  194. </tbody>
  195. </table>
  196. </div>
  197. </body>
  198. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement