In this JavaEE JSP Tutorial, we will use JSF with Managed Bean to store and retrieve data. We will also see how Modal and Rendering works.
Snippet 1: Beans Class
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 |
package tube.codingexamples.beans; //Sample 01: Customer Bean //Make sure the fields follow Java Std. //First letter smaller case. public class Customer { //1.1: Create Fields for Customer (Note: Private) private String custID = ""; private String custName = ""; private String locationZip = ""; //1.2: Bean Setter and Getter public String getCustID() { return custID; } public void setCustID(String custID) { this.custID = custID; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getLocationZip() { return locationZip; } public void setLocationZip(String locationZip) { this.locationZip = locationZip; } //Sample 02: Method to return Customer Information public String getCustomerInfo() { String info = ""; if (custID.trim().length() > 0) info = info + "[" + custID + "]"; if (custName.trim().length() > 0) info = info + " - " + custName + ", "; if (locationZip.trim().length() > 0) info = info + locationZip + "."; return info; } } |
Snippet 2: JSP FIle
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 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!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 03: Use Managed mean JSF controlled JSP Page --> <f:view> <h:form> Customer ID: <h:inputText id="CustId" value="#{customer.custID}"/> <br/> Customer Name: <h:inputText id="custName" value="#{customer.custName}"/> <br/> Zip Code: <h:inputText id="locationZip" value="#{customer.locationZip}"/> <br/> <h:commandButton value="Get Customer Info"/><br/> <h:outputText value="#{customer.customerInfo}" /> </h:form> </f:view> </body> </html> |
Categories: JSP