Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.activemq.web.filter;
 
 import java.util.Map;
 import java.util.Set;
 
 
Exposes Spring ApplicationContexts to JSP EL and other view technologies. Currently a variable is placed in application scope (by default called 'applicationContext') so that POJOs can be pulled out of Spring in a JSP page to render things using EL expressions.
e.g. ${applicationContext.cheese} would access the cheese POJO. Or ${applicationContext.cheese.name} would access the name property of the cheese POJO.
You can then use JSTL to work with these POJOs such as <c.set var="myfoo" value="${applicationContext.foo}"/>
In addition to applicationContext a 'requestContext' variable is created which will automatically bind any request parameters to the POJOs extracted from the applicationContext - which is ideal for POJOs which implement queries in view technologies.

Version:
$Revision: 565003 $
 
 public class ApplicationContextFilter implements Filter {
 
     private ServletContext servletContext;
     private String applicationContextName = "applicationContext";
     private String requestContextName = "requestContext";
     private String requestName = "request";
 
     public void init(FilterConfig configthrows ServletException {
         this. = config.getServletContext();
         this. = getInitParameter(config"applicationContextName");
         this. = getInitParameter(config"requestContextName");
         this. = getInitParameter(config"requestName");
 
         // register the application context in the applicationScope
         WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext();
         Map wrapper = createApplicationContextWrapper(context);
     }
 
     public void doFilter(ServletRequest requestServletResponse responseFilterChain chainthrows IOExceptionServletException {
         // lets register a requestContext in the requestScope
         Map requestContextWrapper = createRequestContextWrapper(request);
         request.setAttribute(requestContextWrapper);
         request.setAttribute(request);
         chain.doFilter(requestresponse);
     }
 
     public void destroy() {
     }
 
     public ServletContext getServletContext() {
         return ;
     }
 
     public String getApplicationContextName() {
         return ;
     }
 
     public void setApplicationContextName(String variableName) {
         this. = variableName;
     }
 
     public String getRequestContextName() {
        return ;
    }
    public void setRequestContextName(String requestContextName) {
        this. = requestContextName;
    }
    protected String getInitParameter(FilterConfig configString keyString defaultValue) {
        String parameter = config.getInitParameter(key);
        return (parameter != null) ? parameter : defaultValue;
    }

    
Creates a wrapper around the web application context so that it can be accessed easily from inside JSP EL (or other expression languages in other view technologies).
    protected Map createApplicationContextWrapper(final WebApplicationContext context) {
        Map wrapper = new AbstractMap() {
            public WebApplicationContext getContext() {
                return context;
            }
            public Object get(Object key) {
                if (key == null) {
                    return null;
                }
                return context.getBean(key.toString());
            }
            public Set entrySet() {
                return .;
            }
        };
        return wrapper;
    }

    
Creates a wrapper around the request context (e.g. to allow POJOs to be auto-injected from request parameter values etc) so that it can be accessed easily from inside JSP EL (or other expression languages in other view technologies).
    protected Map createRequestContextWrapper(final ServletRequest request) {
        final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext();
        Map wrapper = new AbstractMap() {
            public WebApplicationContext getContext() {
                return context;
            }
            public Object get(Object key) {
                if (key == null) {
                    return null;
                }
                return bindRequestBean(context.getBean(key.toString()), request);
            }
            public Set entrySet() {
                return .;
            }
        };
        return wrapper;
    }

    
Binds properties from the request parameters to the given POJO which is useful for POJOs which are configurable via request parameters such as for query/view POJOs
    protected Object bindRequestBean(Object beanServletRequest request) {
        ServletRequestDataBinder binder = new ServletRequestDataBinder(beannull);
        binder.bind(request);
        return bean;
    }
New to GrepCode? Check out our FAQ X