| Method from org.apache.struts2.components.Include Detail: |
public void addParameter(String key,
Object value) {
// don't use the default implementation of addParameter,
// instead, include tag requires that each parameter be a list of objects,
// just like the HTTP servlet interfaces are (String[])
if (value != null) {
List currentValues = (List) parameters.get(key);
if (currentValues == null) {
currentValues = new ArrayList();
parameters.put(key, currentValues);
}
currentValues.add(value);
}
}
|
public boolean end(Writer writer,
String body) {
String page = findString(value, "value", "You must specify the URL to include. Example: /foo.jsp");
StringBuilder urlBuf = new StringBuilder();
// Add URL
urlBuf.append(page);
// Add request parameters
if (parameters.size() > 0) {
urlBuf.append('?');
String concat = "";
// Set parameters
Iterator iter = parameters.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object name = entry.getKey();
List values = (List) entry.getValue();
for (int i = 0; i < values.size(); i++) {
urlBuf.append(concat);
urlBuf.append(name);
urlBuf.append('=');
try {
urlBuf.append(URLEncoder.encode(values.get(i).toString(), "UTF-8"));
} catch (Exception e) {
LOG.warn("unable to url-encode "+values.get(i).toString()+", it will be ignored");
}
concat = "&";
}
}
}
String result = urlBuf.toString();
// Include
try {
include(result, writer, req, res);
} catch (Exception e) {
LOG.warn("Exception thrown during include of " + result, e);
}
return super.end(writer, body);
}
|
public static String getContextRelativePath(ServletRequest request,
String relativePath) {
String returnValue;
if (relativePath.startsWith("/")) {
returnValue = relativePath;
} else if (!(request instanceof HttpServletRequest)) {
returnValue = relativePath;
} else {
HttpServletRequest hrequest = (HttpServletRequest) request;
String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
if (uri == null) {
uri = RequestUtils.getServletPath(hrequest);
}
returnValue = uri.substring(0, uri.lastIndexOf('/')) + '/' + relativePath;
}
// .. is illegal in an absolute path according to the Servlet Spec and will cause
// known problems on Orion application servers.
if (returnValue.indexOf("..") != -1) {
Stack stack = new Stack();
StringTokenizer pathParts = new StringTokenizer(returnValue.replace('\\', '/'), "/");
while (pathParts.hasMoreTokens()) {
String part = pathParts.nextToken();
if (!part.equals(".")) {
if (part.equals("..")) {
stack.pop();
} else {
stack.push(part);
}
}
}
StringBuilder flatPathBuffer = new StringBuilder();
for (int i = 0; i < stack.size(); i++) {
flatPathBuffer.append("/").append(stack.elementAt(i));
}
returnValue = flatPathBuffer.toString();
}
return returnValue;
}
|
public static void include(String aResult,
Writer writer,
ServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String resourcePath = getContextRelativePath(request, aResult);
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
if (rd == null) {
throw new ServletException("Not a valid resource path:" + resourcePath);
}
PageResponse pageResponse = new PageResponse(response);
// Include the resource
rd.include((HttpServletRequest) request, pageResponse);
//write the response back to the JspWriter, using the correct encoding.
String encoding = getEncoding();
if (encoding != null) {
//use the encoding specified in the property file
pageResponse.getContent().writeTo(writer, encoding);
} else {
//use the platform specific encoding
pageResponse.getContent().writeTo(writer, null);
}
}
|
public void setDefaultEncoding(String encoding) {
defaultEncoding = encoding;
}
|
public void setValue(String value) {
this.value = value;
}
|