Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Netbean: Java EE + Maven + Spring + ThymeLeaf approach
- 1. Tạo prj Maven Java EE
- 2. Ở ejb:
- + 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.
- + (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 :)) )
- + new > Enterprise JavaBean > Session Beans For Entity Classes > chọn bảng > tick chọn local và nhập tên package
- + Clean build
- 3. Ở web:
- + Add Spring framework
- + vào pom.xml kiếm "bom" và thêm thuộc tính sau vào cuối dependency đó
- <type>pom</type>
- + Cũng trong pom.xml thêm mấy dependency sau
- <!-- Thymeleaf core -->
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf</artifactId>
- <version>3.0.11.RELEASE</version>
- </dependency>
- <!-- Thymeleaf spring 4 integration -->
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf-spring4</artifactId>
- <version>3.0.11.RELEASE</version>
- </dependency>
- <!-- Làm layout đơn giản hơn -->
- <dependency>
- <groupId>nz.net.ultraq.thymeleaf</groupId>
- <artifactId>thymeleaf-layout-dialect</artifactId>
- <version>2.4.1</version>
- </dependency>
- + Xong rồi build with dependencies
- + Để sử dụng được cả spring và thymeleaf thì config như sau:
- == trong applicationContext.xml, thêm đoạn sau:
- <bean id="templateResolver"
- class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
- <property name="prefix" value="/" />
- <property name="suffix" value=".html" />
- <property name="cacheable" value="true" />
- </bean>
- <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
- <property name="templateEngine" ref="templateEngine" />
- </bean>
- <bean id="templateEngine"
- class="org.thymeleaf.spring4.SpringTemplateEngine">
- <property name="templateResolver" ref="templateResolver" />
- <property name="enableSpringELCompiler" value="true" />
- <property name="additionalDialects">
- <set>
- <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
- </set>
- </property>
- </bean>
- == bên dispatcher-servlet.xml thì như xài Spring bình thường
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <context:annotation-config />
- <!-- Chỉnh lại package ở đây để cho nó tìm controller -->
- <context:component-scan base-package="vn.aptech" />
- <!-- Custom đường dẫn cho resource -->
- <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
- <mvc:resources mapping="/admin_resources/**" location="/admin/resources/" cache-period="31556926"/>
- <mvc:annotation-driven />
- </beans>
- == ở web.xml, tìm chỉnh mấy dòng sau
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>/</welcome-file>
- </welcome-file-list>
- + Để xài EJB bên prj ejb thì config tiếp
- == ở pom.xml, thêm dependency
- <dependency>
- <groupId>com.npht</groupId> <!-- Chỉnh package lại ở đây -->
- <artifactId>SpringThymeLeaf-ejb</artifactId> <!-- Chỉnh tên project ejb ở đây -->
- <version>1.0</version> <!-- Version cũng bắt buộc trùng với project ejb -->
- <scope>provided</scope>
- </dependency>
- == ở web.xml
- <ejb-local-ref>
- <ejb-ref-name>UsersFacade</ejb-ref-name> <!-- Tên để chút nữa khai báo mappedName bên controller -->
- <ejb-ref-type>Session</ejb-ref-type>
- <local>com.npht.sb.UsersFacadeLocal</local> <!-- Đường dẫn package đến class local của nó -->
- </ejb-local-ref>
- + Xong rồi build with dependencies để đưa ejb vào
- + Tạo Java Class tên IndexController đặt vào package ...controller (vn.aptech.controller), nội dung như sau
- @Controller
- // Map controller này là gốc cho đường dẫn: "(Tên prj web)/"
- @RequestMapping("")
- public class IndexController {
- // mappedName là cái ejb-ref-name khai báo ở web.xml
- @EJB(mappedName = "UsersFacade")
- UsersFacadeLocal usersFacade;
- // Map cho đường dẫn: "(đường dẫn gốc controller)/"
- @RequestMapping(value = "", method = RequestMethod.GET)
- public String getHome(ModelMap mm) {
- mm.addAttribute("users", usersFacade.findAll());
- return "index";
- }
- }
- + Tạo trang layout.html ở ngoài Web Pages
- <!-- Khai báo thư viện thymeleaf ở thẻ gốc -->
- <html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:th="http://www.thymeleaf.org"
- xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <!-- Title này sẽ được thay thế nếu như file chính có tag title có nội dung -->
- <title layout:fragment="title">Layout title</title>
- <!-- th:href, th:src,... dùng giá trị nằm trong @{/đường_dẫn_đi_từ_gốc} -->
- <!-- 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) -->
- <link rel="stylesheet" th:href="@{/resources/index.css}"/>
- </head>
- <body>
- <!-- 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 -->
- <header layout:fragment="header">
- This is layout header
- </header>
- <!-- Cũng tương tự như phần header -->
- <div layout:fragment="content">
- This is layout content
- </div>
- <footer layout:fragment="footer">
- This is layout footer
- </footer>
- </body>
- </html>
- + Tạo trang index.html ở ngoài Web Pages
- <!DOCTYPE html>
- <!-- 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}-->
- <html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:th="http://www.thymeleaf.org"
- xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
- layout:decorate="~{layout}">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <!-- thay đổi title -->
- <title>Index header</title>
- </head>
- <body>
- <!-- thay đổi div có fragment content -->
- <div layout:fragment="content">
- <table border="1">
- <thead>
- <tr>
- <td>Username</td>
- <td>Name</td>
- <td>Birthday</td>
- <td>Gender</td>
- <td>Role</td>
- <td>Action</td>
- </tr>
- </thead>
- <tbody>
- <tr th:if="${users eq null or users.empty}">
- <td colspan="6">No users</td>
- </tr>
- <tr th:each="u : ${users}">
- <td th:text="${u.username}"></td>
- <td th:text="${u.name}"></td>
- <td th:text="${#calendars.format(u.birthday, 'dd/MM/yyyy')}"></td>
- <td th:text="${u.isMale} == true?'Male':'Female'"></td>
- <td th:text="${u.isAdmin} == true?'Admin':'User'"></td>
- </tr>
- </tbody>
- </table>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement