alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (AbstractGxpResult.java)

This example Struts source code file (AbstractGxpResult.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

abstractgxpresult, defaultprovider, defaultprovider, gxpresourceprovider, http, httpservletresponse, io, ioexception, ioexception, locale, locale, request, response, result, servlet, string, string, utf-8, util, writer

The Struts AbstractGxpResult.java source code

/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts2.views.gxp;

import com.opensymphony.xwork2.Result;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

/**
 * The abstract base class for our Struts 2 GXP result type implementation. It
 * outputs GXP, and pulls GXP parameters from Struts 2's value stack. Implementing
 * classes have to:
 * <ol>
 * <li>Implement execute(ActionInvocation), which must instruct the
 * GXP to write itself to the output stream. See {@link GxpResult} for a
 * sample implementation.</li>
 * <li>Add a public static final field DEFAULT_PARAM with
 * the value 'gxpName'. Struts 2 needs this to set the name of your
 * template into this object.</li>
 * </ol>
 * <p/>
 * <p>If you want to use instantiated GXPs (using the nested
 * {@code Interface}), you can set the use{@code useInstances} parameter to
 * {@code true}:
 * <pre>
 *     <result-types>
 *       <result-type name="gxp" class="org.apache.struts2.views.gxp.GxpResult">
 *         <param name="useInstances">true</param>
 *       </result-type>
 *     </result-types>
 * </pre>
 * This means that Struts 2 will attempt to instantiate the {@code Interface}
 * using the {@link com.opensymphony.xwork2.ObjectFactory}. If
 * {@link com.google.webwork.GuiceWebWorkIntegrationModule} is installed, or
 * {@link com.google.webwork.ContainerObjectFactory} is set as the static
 * {@code ObjectFactory} instance, then Guice will be used to instantiate the
 * GXP instance; otherwise, only GXPs with no constructor parameters will work.
 *
 * @author Bob Lee
 */
public abstract class AbstractGxpResult implements Result {

    private boolean useInstances = false;
    private String gxpName;

    public void setGxpName(String gxpName) {
        this.gxpName = gxpName;
    }

    protected final String getGxpName() {
        return gxpName;
    }

    public void setUseInstances(boolean useInstances) {
        this.useInstances = useInstances;
    }

    protected final boolean getUseInstances() {
        return useInstances;
    }

    /**
     * Provides resources necessary to execute a GXP.
     */
    protected interface GxpResourceProvider {
        Writer getWriter() throws IOException;

        Locale getLocale();
    }

    /**
     * Uses reasonable defaults to provide resources.
     */
    protected static class DefaultProvider implements GxpResourceProvider {

        private final String contentType;

        public DefaultProvider(String contentType) {
            this.contentType = contentType;
        }

        public Writer getWriter() throws IOException {
            setContentType();
            return ServletActionContext.getResponse().getWriter();
        }

        public Locale getLocale() {
            return ServletActionContext.getRequest().getLocale();
        }

        void setContentType() {
            HttpServletResponse response = ServletActionContext.getResponse();
            // set content type if it hasn't already been set.
            if (response.getContentType() == null || response.getContentType().isEmpty()) {
                response.setContentType(contentType);
            }
            // If no character encoding was set in the content type, default to UTF-8.
            if (response.getCharacterEncoding() == null || response.getCharacterEncoding().isEmpty()) {
                response.setCharacterEncoding("UTF-8");
            }
        }
    }

}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts AbstractGxpResult.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.