Documentation
Examples
Tutorials
JAR files
Leaks
Errata
/ch7/accountLogin/ControllerHelper.java
/ch7/accountLogin/ControllerHelper.java
package ch7.accountLogin; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import shared.HibernateHelper; import shared.HelperBaseCh6; import shared.ButtonMethod; public class ControllerHelper extends HelperBaseCh6 { protected RequestDataAccount data = new RequestDataAccount(); public ControllerHelper(HttpServletRequest request, HttpServletResponse response) { super(request, response); } static public void initHibernate(boolean create) { if (create) { HibernateHelper.createTable(RequestDataAccount.class); } HibernateHelper.initSessionFactory(RequestDataAccount.class); } public Object getData() { return data; } public void copyFromSession(Object sessionHelper) { if (sessionHelper.getClass() == this.getClass()) { data = ((ControllerHelper) sessionHelper).data; checked = ((ControllerHelper) sessionHelper).checked; selected = ((ControllerHelper) sessionHelper).selected; } } public String jspLocation(String page) { return "/WEB-INF/classes/ch7/accountLogin/" + page; } @ButtonMethod(isDefault = true) public String getMethod() { return jspLocation("Login.jsp"); } @ButtonMethod(buttonName = "loginButton") public String loginMethod() { String address; fillBeanFromRequest(data); setErrors(data); if (isValidProperty("accountNumber")) { Object dataPersistent = HibernateHelper.getFirstMatch(data, "accountNumber", data.getAccountNumber()); if (dataPersistent != null) { data = (RequestDataAccount) dataPersistent; } //Not in book. All NotNull annotations will generate //an error for the first time the page is displayed. //The NotNull annotations have be added back to the //bean for this example. getErrors().clear(); address = "Edit.jsp"; } else { address = "Login.jsp"; } return jspLocation(address); } @ButtonMethod(buttonName = "newUserButton") public String newUserMethod() { data = new RequestDataAccount(); return jspLocation("Login.jsp"); } @ButtonMethod(buttonName = "editButton") public String editMethod() { return jspLocation("Edit.jsp"); } @ButtonMethod(buttonName = "confirmButton") public String confirmMethod() { //Create new data and populate from the query string fillBeanFromRequest(data); //The next JSP address depends on the //validity of the data. String address; if (isValid(data)) { address = "Confirm.jsp"; } else { address = "Edit.jsp"; } return jspLocation(address); } @ButtonMethod(buttonName = "processButton") public String processMethod() { if (!isValid(data)) { return jspLocation("Expired.jsp"); } HibernateHelper.updateDB(data); List list = HibernateHelper.getListData(RequestDataAccount.class); request.setAttribute("database", list); return jspLocation("Process.jsp"); } @Override protected void doGet() throws ServletException, java.io.IOException { if (HibernateHelper.testDB(response)) { addHelperToSession("helper", SessionData.IGNORE); //Edit.jsp is the only page that will //be displayed from a GET request. String address = getMethod(); request.getRequestDispatcher(address).forward(request, response); } } @Override protected void doPost() throws ServletException, java.io.IOException { if (HibernateHelper.testDB(response)) { addHelperToSession("helper", SessionData.READ); String address = executeButtonMethod(); request.getRequestDispatcher(address).forward(request, response); } } }
Contact the author