In this video, we will learn how to traverse JPA 1-1 composite Entity in the JSP file.
JSP for Entity Collection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!-- Sample 6.17: Required Imports --> <%@ page import="java.util.List"%> <%@ page import="tube.coding.examples.jpa.entity.SavingsAccount"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% //Sample 6.18: Retrieve the accounts List from request attribute List<SavingsAccount> accounts = null; accounts = (List<SavingsAccount>) request.getAttribute("Accounts"); %> <h1>List of Accounts</h1> <% //Sample 6.19: Iterate the List and display result with //alternate div tag style int count = accounts.size(); for (int index = 0; index < count; index++) { SavingsAccount account = accounts.get(index); if ((index+1) % 2 == 0) { %> <!-- Sample 7.13a: Include Location in Result --> <div style="font-size: large; color: blue; background-color: aliceblue; font-family: Verdana;"> The account id <%=account.getPid()%> is owned by <%=account.getpName()%> and lives in <%=account.getLoc() %>. <br> Balance is: <%=account.getBal()%> </div> <% } else { %> <!-- Sample 7.13b: Include Location in Result --> <div style="font-size: large; color: blue; background-color: lemonchiffon; font-family: Verdana;"> The account id <%=account.getPid()%> is owned by <%=account.getpName()%> and lives in <%=account.getLoc() %>. <br> Balance is: <%=account.getBal()%></div> <% } } %> </body> </html> |
JSP For Single Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!-- Sample 6.08: Required below imports --> <%@ page import="tube.coding.examples.jpa.entity.SavingsAccount" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% //Sample 6.09: Get the JPA Account instance from the Request Context SavingsAccount account = (SavingsAccount) request.getAttribute("Account"); %> <!-- Sample 6.10: Present the output to Browser --> <h1> Query By Id : Result</h1> <div style="font-size: large; color: blue; background-color: aliceblue; font-family: Verdana;"> The account id <span style="text-decoration: underline;"> <%= account.getPid() %> </span> is owned by <span style="text-decoration: underline;"> <%= account.getpName() %> </span> . Balance is: <span style="text-decoration: underline;"> <%= account.getBal() %> . </span> <!-- Sample 7.14: Include Location in Result --> <p> The Person Lives in <br> <span style="text-decoration: underline;"> <%= account.getLoc().getStreet() %> </span> located in <span style="text-decoration: underline;"> <%= account.getLoc().getCity() %> </span> </p> </div> </body> </html> |
Categories: JavaEE-EJB-Tube