All Downloads are FREE. Search and download functionalities are using the official Maven repository.

webit.script.support.struts.WebitScriptResultSupport Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
// Copyright (c) 2013, Webit Team. All Rights Reserved.
package webit.script.support.struts;

import com.opensymphony.xwork2.ActionInvocation;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import webit.script.web.WebEngineManager;
import webit.script.web.WebEngineManager.ServletContextProvider;

/**
 *
 * @author Zqq
 */
public class WebitScriptResultSupport extends StrutsResultSupport {

    private final static WebEngineManager engineManager;

    static {
        engineManager = new WebEngineManager(new ServletContextProvider() {

            public ServletContext getServletContext() {
                return ServletActionContext.getServletContext();
            }
        });
    }

    private static String contentType;
    public static void setConfigPath(String configPath) {
        engineManager.setConfigPath(configPath);
    }

    public static void setContentType(String contentType) {
        WebitScriptResultSupport.contentType = contentType;
    }
    
    public static void resetEngine() {
        engineManager.resetEngine();
    }

    @Override
    protected void doExecute(String view, ActionInvocation ai) throws Exception {
        Map model = ai.getStack().getContext();
        HttpServletRequest request = (HttpServletRequest) model.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) model.get(ServletActionContext.HTTP_RESPONSE);
        if (contentType != null) {
            response.setContentType(contentType);
        }
        Map params = new HashMap();
        params.put("request", request);
        params.put("response", response);
        params.put("stackContext", model);
        params.put("action", ai.getAction());
        engineManager.renderTemplate(view, params, response);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy