Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Netbean: Java EE + Spring approach
- 1. Tạo prj 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. Ở war:
- + Add Spring framework
- + vào Web Pages>dispatcher-servlet.xml và copy đè nội dung sau vào
- <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 />
- <context:component-scan base-package="vn.aptech.controller" />
- <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
- <mvc:annotation-driven />
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/"
- p:suffix=".jsp" />
- + Tạo Java Class tên testController đặt vào package ...controller
- Đặt 2 annotation trên đầu như sau
- @Controller
- @RequestMapping("")
- public class testController {
- }
- + Dùng insert code để lấy enterprise bean bên ejb qua, nó tựa tựa như sau
- private EmployeeFacadeLocal employeeFacade = lookupEmployeeFacadeLocal();
- private EmployeeFacadeLocal lookupEmployeeFacadeLocal() {
- try {
- Context c = new InitialContext();
- return (EmployeeFacadeLocal) c.lookup("java:global/SpringTest/SpringTest-ejb/EmployeeFacade!vn.aptech.sb.EmployeeFacadeLocal");
- } catch (NamingException ne) {
- Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
- throw new RuntimeException(ne);
- }
- }
- + thêm 1 method trả về dạng string để đưa ra trang index,
- @RequestMapping(value = "", method = RequestMethod.GET) // mapping bằng cái này, nếu value="home" thì sẽ truy cập bằng .../home
- public String viewHome(ModelMap mm) {
- mm.put("empList", employeeFacade.findAll()); // Cái này truyền như request.setAttribute
- 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
- }
- + chỉnh lại web.xml
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>/</welcome-file>
- </welcome-file-list>
- + 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/....
- + vào thư mục WEB-INF tạo thư mục pages rồi thêm index.jsp vào
- Khai báo thư viện bootstrap
- <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css"/>
- hoặc thích xài tag thì
- <link rel="stylesheet" href="<c:url value="/resources/css/bootstrap.min.css"/>"/>
- content:
- <h1>Employee's list</h1>
- <table class="table table-responsive table-striped">
- <thead>
- <tr>
- <th>ID</th>
- <th>Name</th>
- <th>Age</th>
- <th>Gender</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <c:forEach var="item" items="${empList}">
- <tr>
- <td>${item.empID}</td>
- <td>${item.empName}</td>
- <td>${item.empAge}</td>
- <td>
- <c:choose>
- <c:when test="${item.gender == false}">
- Male
- </c:when>
- <c:otherwise>
- Female
- </c:otherwise>
- </c:choose>
- </td>
- <td></td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement