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

ぃ、小莉子 提交于 2019-12-19 02:05:41

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/3952755/how-to-obtain-model-attribute-or-springs-bean-in-sitemesh-decorator

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