FileDocCategorySizeDatePackage
Include.javaAPI DocExample14328Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Include

public class Include extends Component

Include a servlet's output (result of servlet or a JSP page).

Note: Any additional params supplied to the included page are not accessible within the rendered page through the <s:property...> tag!

  • value* (String) - jsp page to be included

Examples


<-- One: -->
<s:include value="myJsp.jsp" />

<-- Two: -->
<s:include value="myJsp.jsp">
<s:param name="param1" value="value2" />
<s:param name="param2" value="value2" />
</s:include>

<-- Three: -->
<s:include value="myJsp.jsp">
<s:param name="param1">value1</s:param>
<s:param name="param2">value2<s:param>
</s:include>



Example one - do an include myJsp.jsp page
Example two - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2
Example three - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2

Fields Summary
private static final Log
_log
private static String
encoding
private static boolean
encodingDefined
protected String
value
private HttpServletRequest
req
private HttpServletResponse
res
private static String
defaultEncoding
Constructors Summary
public Include(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest req, HttpServletResponse res)


           
        super(stack);
        this.req = req;
        this.res = res;
    
Methods Summary
public voidaddParameter(java.lang.String key, java.lang.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 booleanend(java.io.Writer writer, java.lang.String body)

        String page = findString(value, "value", "You must specify the URL to include. Example: /foo.jsp");
        StringBuffer urlBuf = new StringBuffer();

        // 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) {
            LogFactory.getLog(getClass()).warn("Exception thrown during include of " + result, e);
        }

        return super.end(writer, body);
    
public static java.lang.StringgetContextRelativePath(javax.servlet.ServletRequest request, java.lang.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);
                    }
                }
            }

            StringBuffer flatPathBuffer = new StringBuffer();

            for (int i = 0; i < stack.size(); i++) {
                flatPathBuffer.append("/").append(stack.elementAt(i));
            }

            returnValue = flatPathBuffer.toString();
        }

        return returnValue;
    
private static java.lang.StringgetEncoding()
Get the encoding specified by the property 'struts.i18n.encoding' in struts.properties, or return the default platform encoding if not specified.

Note that if the property is not initially defined, this will return the system default, even if the property is later defined. This is mainly for performance reasons. Undefined properties throw exceptions, which are a costly operation.

If the property is initially defined, it is read every time, until is is undefined, and then the system default is used.

Why not cache it completely? Some applications will wish to be able to dynamically set the encoding at runtime.

return
The encoding to be used.

        if (encodingDefined) {
            try {
                encoding = defaultEncoding;
            } catch (IllegalArgumentException e) {
                encoding = System.getProperty("file.encoding");
                encodingDefined = false;
            }
        }

        return encoding;
    
public static voidinclude(java.lang.String aResult, java.io.Writer writer, javax.servlet.ServletRequest request, javax.servlet.http.HttpServletResponse response)

        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 static voidsetDefaultEncoding(java.lang.String encoding)

        defaultEncoding = encoding;
    
public voidsetValue(java.lang.String value)

        this.value = value;