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

陌路散爱 提交于 2019-11-30 20:03:06

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.

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!