J2EE - case study (by pew)
---------------------JavaBeans--------------------------- #ProcessListItems.java(servlet) ArrayList loanList = (ArrayList) patron.retrieveLoanedCopies(); if (loanList.size() > 0) { LoanedCopyListBean listBean = new LoanedCopyListBean(); listBean.setLoanedCopyList(loanList); session.setAttribute("listitems", listBean); context.getRequestDispatcher("/WEB-INF/jsp/ListItems.jsp").forward(req, resp); } #ListItems.jsp
<%for (int i = 0; i < listitems.getLoanedCopyList().size(); i++) { pageContext.setAttribute("item", listitems.getLoanedCopyList(i));%>
<%}%> ---------------------Using JSP Expressioon Language (EL)----------- #ListItems.jsp
<%for (int i = 0; i < listitems.getLoanedCopyList().size(); i++) { pageContext.setAttribute("item", listitems.getLoanedCopyList(i));%>
${item.author}
${item.title}
<%}%> --------------------------JSP Custom Taggs-------------------------- #ListItems.jsp <%@taglib uri="/WEB-INF/tld/taglib.tld" prefix="ilib"%>
${item.author}
${item.title}
#/WEB-INF/tld/taglib.tld
1.2
2.0
ilib
hilite
com.ibm.library.tags.MarkRenewedTag
name
true
color
false
#com.ibm.library.tags.MarkRenewedTag package com.ibm.library.tags; import ...; public class MarkRenewedTag extends SimpleTagSupport { private String color = "green"; private String name; public MarkRenewedTag() { super();} public void setColor(String color) {this.color = color;} public void setName(String name) { this.name = name;} public void doTag() throws JspException, IOException { Object obj = getJspContext().findAttribute(name); if (obj == null) { throw new JspException("Hilite tag: Name " + name + " not found"); } // Is this an object of type com.ibm.library.LoanedCopy? if (!obj.getClass().equals(com.ibm.library.model.LoanedCopy.class)) { throw new JspException("Hilite tag: Name is not of type " + "com.ibm.library.model.LoanedCopy"); } // Set flag if the item was in fact renewed boolean renewed = ((LoanedCopy) obj).isRenewAccomplished(); // Process tag body StringWriter bodyText = new StringWriter(); JspFragment body = getJspBody(); if (body != null) { body.invoke(bodyText); StringBuffer buffer = new StringBuffer(); // Place font and bold tags at start of body // if renewed flag set if (renewed) { buffer.append("
"); } // Copy in the body for either case buffer.append(bodyText.toString()); // Place font end tag at end of body if renewed if (renewed) { buffer.append("
"); } try { JspWriter out = getJspContext().getOut(); out.write(buffer.toString()); } catch (IOException e) { throw new JspException("Hilite tag: " + e.getMessage()); } } } } (JavaServer Pages Standard Tag Library(JSTL):
)