📄 htmlportal.java
字号:
package personalPortal;/** * Title: Professional Java Servlet Programming - Chapter 2 * Description: HTML support class for PortalServlet * Copyright: Copyright (c) 2001 * Company: * @author Andrew Harbourne-Thomas * @version 1.0 */public class HTMLPortal implements DataMapping { /** Variable to build the web page opening HTML */ private StringBuffer openPage; /** Variable to build the web page closing HTML */ private StringBuffer closePage; /** Stores the parameter indicating the page requested */ private String pageRequested; /** Stores the parameter indicating if the client wants * the normal view or print view */ private String pageViewType; /** * Constructor builds the page ready for use. It calls the methods * to prepare the various parts of the page. * * @param pageRequested - the specific page requested * @param pageViewType - type of view (print or normal) requested */ public HTMLPortal(String pageRequested, String pageViewType) { this.pageRequested = pageRequested; this.pageViewType = pageViewType; //check if we are constructing a normal or print page if (PORTAL_VIEW_PARAM.equalsIgnoreCase(pageViewType)) { prepareOpenPage(); } else { preparePrintOpenPage(); } //prepare the closing part of the page prepareClosePage(); } /** * @return returns the HTML to open the page (title, menu) */ public StringBuffer getOpenPage() { return openPage; } /** * @return returns the HTML to close the page */ public StringBuffer getClosePage() { return closePage; } /** * @return used to prepare HTML for an error */ public StringBuffer getError(String message) { StringBuffer error = new StringBuffer(); error.append("<h2><font color=\"darkred\">"); error.append(message); error.append("</font></h2>"); return error; } /** * this method prepares the normal opening page, including title * and menu. */ private void prepareOpenPage() { openPage = new StringBuffer(); openPage.append("<html><head><title>Personal Portal"); openPage.append("</title></head><body>"); openPage.append("<table width=\"100%\"><tr>"); openPage.append("<td colspan=2 align=\"center\" bgcolor=\"#ffff80\">"); openPage.append("<h1>Personal Portal</h1></td></tr><tr>"); openPage.append("<td width=\"20%\" bgcolor=\"#ffffa0\"><br>"); openPage.append("<b>Menu</b><br><br>"); openPage.append(prepareMenu(false)); openPage.append("<br><br><br><br><br><br><br><br><br>"); openPage.append("</ul></td>"); openPage.append("<td width=\"80%\" valign=\"top\" bgcolor=\"#ffffe0\">"); openPage.append("<center><i><font size=\"-1\">("); openPage.append(preparePrintLink("Print this Page", pageRequested)); openPage.append(")</font></i></center><br>"); openPage.append(""); openPage.append(""); } /** * used to prepare the opening part of the print view web page */ private void preparePrintOpenPage() { openPage = new StringBuffer(); openPage.append("<html><head><title>Personal Portal Print Page"); openPage.append("</title></head><body>"); openPage.append("<table width=\"100%\"><tr>"); openPage.append("<td align=\"center\" bgcolor=\"#ffff80\">"); openPage.append("<h1>Personal Portal</h1></td></tr><tr>"); openPage.append("<td align=\"center\" bgcolor=\"#ffffa0\">"); openPage.append(prepareMenu(true)); openPage.append("</td></tr><tr>"); openPage.append("<td bgcolor=\"#ffffe0\">"); } /** * returns the HTML to close the web page */ private void prepareClosePage() { closePage = new StringBuffer(); closePage.append("</td></tr></table>"); closePage.append("<p align=\"right\">© 2002</body></html> "); } /** * This method prepares a HTML menu for either a print view * or normal view of the web page * * @param printMenu - boolean to indicate if this is a menu for * print page or normal page * @return the html menu */ private StringBuffer prepareMenu(boolean printMenu) { StringBuffer menu = new StringBuffer(); StringBuffer menuBreak = new StringBuffer(); if (printMenu) { menuBreak.append(" "); menu.append("<a href=\"javascript:history.go(-1)\">Back</a>"); } else { menuBreak.append("<br><br>"); } //loop through the RESOURCE_DATA 2D array to prepare the //menu for (int i = 0; i < RESOURCE_DATA.length; i++) { menu.append(menuBreak); if (RESOURCE_DATA[i][NAME].equalsIgnoreCase(pageRequested)) { menu.append(RESOURCE_DATA[i][NAME]); } else { menu.append("<a href=\""); menu.append(prepareUrl(PORTAL_VIEW_PARAM, RESOURCE_DATA[i][NAME])); menu.append("\">"); menu.append(RESOURCE_DATA[i][NAME]); menu.append("</a>"); } } return menu; } /** * this method prepares the print version opening page, including title * and menu. */ private StringBuffer preparePrintLink(String caption, String url) { StringBuffer link = new StringBuffer(); link.append("<a href=\""); link.append(prepareUrl(PRINT_VIEW_PARAM, url)); link.append("\">"); link.append(caption); link.append("</a>"); return link; } /** * We use this method to prepare a URL, by appending the required * parameters to the query string. * * @param printChoice - the parameter to indicate if this is a * link to a normal or print view version of the page * @param viewChoice - the parameter to indicate the page * being linked to */ private StringBuffer prepareUrl(String printChoice, String viewChoice) { StringBuffer url = new StringBuffer(); url.append(PORTAL_RESOURCE); url.append("?").append(PORTAL_VIEW_PARAM_NAME); url.append("=").append(printChoice); url.append("&").append(PAGE_REQUESTED_NAME); url.append("=").append(viewChoice); return url; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -