MenuItemTagpublic class MenuItemTag extends BodyTagSupport implements ParamParentThis class is a custom action for inserting HTML references
in a navigation menu. If the action is used in a page
with requested with a URI that corresponds to the page
attribute, only the HTML text is included, otherwise an
HTML reference (...) element is used to enclose the
HTML text. The action also encodes the page URI for URL rewriting
with possible parameter values URL encoded. |
Fields Summary |
---|
private String | page | private String | html | private Vector | params |
Methods Summary |
---|
public int | doAfterBody()Sets the html attribute.
html = bodyContent.getString();
return SKIP_BODY;
| public int | doEndTag()Writes the HTML to the current output, enclosed in an
HTML reference element (...) if the page attribute
doesn't correspond to the current page. If a reference
is used, appends possible URL encoded parameters to the main URL,
encodes the result for URL rewriting and writes the result
to the JspWriter.
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
String requestURI = request.getServletPath();
// Convert the specified page URI to a context-relative URI
String pageURI = toContextRelative(page, requestURI);
String text = null;
if (requestURI.equals(pageURI)) {
text = html;
}
else {
// Add the text as an HTML reference if page is not current page
String contextPath = request.getContextPath();
StringBuffer encodedURL =
new StringBuffer(contextPath + pageURI);
if (params != null && params.size() > 0) {
encodedURL.append('?");
boolean isFirst = true;
Enumeration e = params.elements();
while (e.hasMoreElements()) {
Param p = (Param) e.nextElement();
if (!isFirst) {
encodedURL.append('&");
}
encodedURL.append(p.getName()).append('=").
append(p.getValue());
isFirst = false;
}
}
HttpServletResponse res =
(HttpServletResponse) pageContext.getResponse();
text = "<a href=\"" + res.encodeURL(encodedURL.toString()) +
"\">" + html + "</a>";
}
try {
JspWriter out = pageContext.getOut();
out.print(text);
}
catch (IOException e) {}
return EVAL_PAGE;
| public void | release()Releases all instance variables.
page = null;
html = null;
params = null;
super.release();
| public void | setPage(java.lang.String page)Sets the page attribute.
this.page = page;
| public void | setParam(java.lang.String name, java.lang.String value)Adds a parameter name and value. This method is called by param
tags in the action body.
if (params == null) {
params = new Vector();
}
Param param = new Param(name, value);
params.addElement(param);
| private java.lang.String | toContextRelative(java.lang.String relURI, java.lang.String currURI)Returns a page-relative or context-relative path URI as
a context-relative URI.
if (relURI.startsWith("/")) {
// Must already be context-relative
return relURI;
}
String origRelURI = relURI;
if (relURI.startsWith("./")) {
// Remove current dir characters
relURI = relURI.substring(2);
}
String currDir = currURI.substring(0, currURI.lastIndexOf("/") + 1);
StringTokenizer currLevels = new StringTokenizer(currDir, "/");
// Remove and count all parent dir characters
int removeLevels = 0;
while (relURI.startsWith("../")) {
if (relURI.length() < 4) {
throw new JspException("Invalid relative URI: " + origRelURI);
}
relURI = relURI.substring(3);
removeLevels++;
}
if (removeLevels > currLevels.countTokens()) {
throw new JspException("Invalid relative URI: " + origRelURI +
" points outside the context");
}
int keepLevels = currLevels.countTokens() - removeLevels;
StringBuffer newURI = new StringBuffer("/");
for (int j = 0; j < keepLevels; j++) {
newURI.append(currLevels.nextToken()).append("/");
}
return newURI.append(relURI).toString();
|
|