Advertisement
haithienht

Netbean: JavaEE + Spring MVC Approach

Nov 8th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. Netbean: Java EE + Spring approach
  2. 1. Tạo prj 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. Ở war:
  10. + Add Spring framework
  11. + vào Web Pages>dispatcher-servlet.xml và copy đè nội dung sau vào
  12.  
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15. xmlns:p="http://www.springframework.org/schema/p"
  16. xmlns:aop="http://www.springframework.org/schema/aop"
  17. xmlns:tx="http://www.springframework.org/schema/tx"
  18. xmlns:context="http://www.springframework.org/schema/context"
  19. xmlns:mvc="http://www.springframework.org/schema/mvc"
  20. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  21. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  22. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  23. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  24. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  25.  
  26. <context:annotation-config />
  27. <context:component-scan base-package="vn.aptech.controller" />
  28.  
  29. <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
  30. <mvc:annotation-driven />
  31.  
  32. <bean id="viewResolver"
  33. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  34. p:prefix="/WEB-INF/"
  35. p:suffix=".jsp" />
  36.  
  37. + Tạo Java Class tên testController đặt vào package ...controller
  38. Đặt 2 annotation trên đầu như sau
  39.  
  40. @Controller
  41. @RequestMapping("")
  42. public class testController {
  43. }
  44.  
  45. + Dùng insert code để lấy enterprise bean bên ejb qua, nó tựa tựa như sau
  46.  
  47. private EmployeeFacadeLocal employeeFacade = lookupEmployeeFacadeLocal();
  48. private EmployeeFacadeLocal lookupEmployeeFacadeLocal() {
  49. try {
  50. Context c = new InitialContext();
  51. return (EmployeeFacadeLocal) c.lookup("java:global/SpringTest/SpringTest-ejb/EmployeeFacade!vn.aptech.sb.EmployeeFacadeLocal");
  52. } catch (NamingException ne) {
  53. Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
  54. throw new RuntimeException(ne);
  55. }
  56. }
  57.  
  58. + thêm 1 method trả về dạng string để đưa ra trang index,
  59. @RequestMapping(value = "", method = RequestMethod.GET) // mapping bằng cái này, nếu value="home" thì sẽ truy cập bằng .../home
  60. public String viewHome(ModelMap mm) {
  61. mm.put("empList", employeeFacade.findAll()); // Cái này truyền như request.setAttribute
  62. return "pages/index"; // Cái này sẽ trỏ đến thư mục WEB-INF/pages/index.jsp nếu theo config xml như trên
  63. }
  64.  
  65. + chỉnh lại web.xml
  66.  
  67. <servlet-mapping>
  68. <servlet-name>dispatcher</servlet-name>
  69. <url-pattern>/</url-pattern>
  70. </servlet-mapping>
  71.  
  72. <welcome-file-list>
  73. <welcome-file>/</welcome-file>
  74. </welcome-file-list>
  75.  
  76. + Có thể thêm thư viện bootstrap vào thư mục Web Pages/resources (tùy config xml ở trên) và truy cập bằng (WebContext)/resources/....
  77.  
  78. + vào thư mục WEB-INF tạo thư mục pages rồi thêm index.jsp vào
  79. Khai báo thư viện bootstrap
  80. <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css"/>
  81. hoặc thích xài tag thì
  82. <link rel="stylesheet" href="<c:url value="/resources/css/bootstrap.min.css"/>"/>
  83.  
  84. content:
  85. <h1>Employee's list</h1>
  86. <table class="table table-responsive table-striped">
  87. <thead>
  88. <tr>
  89. <th>ID</th>
  90. <th>Name</th>
  91. <th>Age</th>
  92. <th>Gender</th>
  93. <th>Action</th>
  94. </tr>
  95. </thead>
  96. <tbody>
  97. <c:forEach var="item" items="${empList}">
  98. <tr>
  99. <td>${item.empID}</td>
  100. <td>${item.empName}</td>
  101. <td>${item.empAge}</td>
  102. <td>
  103. <c:choose>
  104. <c:when test="${item.gender == false}">
  105. Male
  106. </c:when>
  107. <c:otherwise>
  108. Female
  109. </c:otherwise>
  110. </c:choose>
  111. </td>
  112. <td></td>
  113. </tr>
  114. </c:forEach>
  115. </tbody>
  116. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement