Package jst

Examples of jst.ScriptRuntime


    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            String scriptName = getScriptName(servletRequest);
            ScriptRuntime runtime = initializeScript( scriptName, (HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse );
            writeResponse( servletResponse, runtime.invoke() );
        } catch( TemplateException ex ) {
            writeScriptError( (HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse, ex );
        }
    }
View Full Code Here


            return ((HttpServletRequest)servletRequest).getRequestURI();
        }
    }

    private void writeScriptError(HttpServletRequest request, HttpServletResponse response, TemplateException ex) throws IOException {
        ScriptRuntime runtime = templateContext.load( "templates/exception.jst");
        runtime.addGlobalVariable( "request", request );
        runtime.addGlobalVariable( "response", response );
        runtime.addGlobalVariable( "servletContext", servletContext );
        runtime.addVariable("ex", ex );
        writeResponse( response, runtime.invoke() );
    }
View Full Code Here

        runtime.addVariable("ex", ex );
        writeResponse( response, runtime.invoke() );
    }

    private ScriptRuntime initializeScript(String scriptName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ScriptRuntime runtime = templateContext.load( scriptName );

        Enumeration attributes = request.getAttributeNames();
        while( attributes.hasMoreElements() ) {
            String name = (String) attributes.nextElement();
            if( name.startsWith( TemplateDispatcher.JST_MIXIN ) ) {
                String mixinName = name.substring( TemplateDispatcher.JST_MIXIN.length() );
                runtime.mixin( mixinName, request.getAttribute( name ) );
            } else if( name.equalsIgnoreCase( TemplateDispatcher.JST_LAYOUT ) ) {
                runtime.setLayout( request.getAttribute( name ).toString() );
            } else if( name.startsWith(TemplateDispatcher.JST_VARIABLE) ) {
                String varName = name.substring( TemplateDispatcher.JST_VARIABLE.length() );
                runtime.addVariable( varName, request.getAttribute( name ) );
            } else if( name.startsWith( TemplateDispatcher.JST_SCRIPT_MIXIN ) ) {
                runtime.include( request.getAttribute( name ).toString() );
            }
        }

        runtime.addGlobalVariable( "request", request );
        runtime.addGlobalVariable( "response", response );
        runtime.addGlobalVariable( "servletContext", servletContext );

        return runtime;
    }
View Full Code Here

    }

    public ScriptRuntime load( String url ) throws IOException {
        initializeContext();

        ScriptRuntime runtime = context.load( url );

        for( String name : variables.keySet() ) {
            runtime.addVariable( name, variables.get( name ) );
        }

        for( String name : mixins.keySet() ) {
            runtime.mixin( name, mixins.get( name ) );
        }

        return runtime;
    }
View Full Code Here

        return runtime;
    }

    public Object evaluate( String url, Map<String,Object> data ) throws IOException {
        ScriptRuntime runtime = load(url);
        for( String key : data.keySet() ) {
            runtime.addVariable( key, data.get(key) );
        }
        return runtime.invoke();
    }
View Full Code Here

        this.templates = templates;
    }

    protected void renderMergedOutputModel(Map objects, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        try {
            ScriptRuntime runtime = templates.load( this.getUrl() );
            runtime.addGlobalVariable( "request", httpServletRequest );
            runtime.addGlobalVariable( "response", httpServletResponse );
            runtime.addGlobalVariable( "servletContext", getServletContext() );

            for( String name : ((Map<String,Object>)objects).keySet() ) {
                runtime.addVariable( name, objects.get( name ) );
            }

            Object value = runtime.invoke();

            writeResponse(httpServletResponse, value);
        } catch( TemplateException ex ) {
            ScriptRuntime runtime = templates.load( "templates/exception.jst" );

            runtime.addGlobalVariable( "request", httpServletRequest);
            runtime.addGlobalVariable( "response", httpServletResponse );
            runtime.addGlobalVariable( "servletContext", getServletContext() );
            runtime.addVariable("ex", ex );

            writeResponse( httpServletResponse, runtime.invoke() );
        }
    }
View Full Code Here

public class EmbedTemplateTest {

    public static void main(String[] args) throws IOException {
        TemplateContextImpl context = new TemplateContextImpl();
        context.addLoader( new FileTemplateLoader( new File("./test") ) );
        ScriptRuntime runtime = context.load("/embedded-test.jst");
        Object output = runtime.addVariable("name", "Charlie").addVariable("age", 31).invoke();

        System.out.println( output );
    }
View Full Code Here

TOP

Related Classes of jst.ScriptRuntime

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.