How to obtain model attribute or spring's bean in sitemesh decorator?
Asked Answered
C

4

8

I am using Spring 3 with sitemesh. I would like to refer to spring context bean in decorator page defined in sitemesh.

The problem is that SiteMesh filter is working outside the Spring context, so request object on sitemesh decorator jsp page is native HttpServletRequest and not wrapper with useful functions to access context and etc.

Is there a way to somehow configure both spring and sitemesh to have access to Spring context in decorator page?

Cheeseparing answered 17/10, 2010 at 9:56 Comment(2)
If you include some of your configuration, specially view resolvers, maybe someone can help you more easily. If you disable Sitemesh filter your exposed beans are accesible? I've never used Sitemesh, but I had a similar problem with Tiles: #2848915Walkout
He shouldn't need to include any of that - the problem is not based on configuration, it's because SiteMesh decorates the pages outside of the Spring ContextWitty
A
3

I had the same issue and solved my problem by using a filter. I created an environment filter that I could use for setting environment data for all requests. Autowire the bean you need to have access too in the filter.

@Component
public class EnvironmentFilter extends OncePerRequestFilter {

    @Autowired
    Object bean;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        request.setAttribute("bean", bean); // add bean or just specific properties of bean.

        filterChain.doFilter(request, response);

    }

}

Configure the filter in web.xml, be sure to use the same pattern for the filter mapping as you have for Sitemesh filter.

<filter>
    <filter-name>environmentFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>environmentFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

The attributes set from your filter are now available from your decorator page.

Aurar answered 4/1, 2013 at 16:21 Comment(0)
W
0

Start by creating a singleton for whatever you fancy, I am just setting a String, but any Class will work:

public class MySiteEnvironment {

    private String someConfigurationParameter;

    public String getSomeConfigurationParameter() {
        return someConfigurationParameter;
    }

    public void setSomeConfigurationParameter(String someConfigurationParameter) {
        this.someConfigurationParameter = someConfigurationParameter;
    }

    /* SINGLETON */
    private static final MySiteEnvironment INSTANCE = new MySiteEnvironment();

    private MySiteEnvironment() {
    }

    public static MySiteEnvironment getInstance() {
        return INSTANCE;
    }
}

Next you need to inject the value:

<bean id="mySiteEnvironment" class="MySiteEnvironment" factory-method="getInstance">
        <property name="someConfigurationParameter" value="myValueOrBean"/>
    </bean>

Finally you access it by:

<%@ page import="MySiteEnvironment" %>
<% pageContext.setAttribute("env", MySiteEnvironment.getInstance()); %> 

Now you can use expression language to access the environment

Witty answered 6/4, 2011 at 19:40 Comment(0)
C
0

I'm not aware of a way to do what you're asking, but there's another alternative as well. You can declare the HttpServletRequest in your controller method parameters. Just put the model objects on the request if they need to be available to Sitemesh. The JSP code looks exacty the same whether the backing context is the servlet request or the Spring MVC model.

Centripetal answered 17/8, 2011 at 4:13 Comment(0)
T
0

I resolved this problem reimplementing the sitemesh filter:

@Component
class SitemeshSpringFilter extends PageFilter implements ApplicationContextAware {
    ApplicationContext applicationContext;

    @Override
    public void doFilter(ServletRequest rq, ServletResponse rs,
            FilterChain chain) throws IOException, ServletException {
        def newRq = new ContextExposingHttpServletRequest(
                rq, getApplicationContext(), null);

        super.doFilter(newRq, rs, chain);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
    throws BeansException {
        this.applicationContext = applicationContext;
    }
}

In the web.xml, declare this filter:

<filter>
    <filter-name>sitemeshSpringFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>sitemeshSpringFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Now, the sitemesh filter will use ContextExposingHttpServletRequest instead normal request.

Taskwork answered 3/5, 2013 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.