Accessing Spring beans from a Tiles view (JSP)

孤街浪徒 提交于 2019-11-27 12:48:25
  • did you google for spring tiles integration? – Bozho May 17 '10 at 11:20
  • Yes, I googled. I have some troubles with previous versions, but now it was all ok, except for this issue. I searched for it but I've found nothing :( – sinuhepop May 17 '10 at 11:47
  • 2
    man, spring and apache must write you a check for this ;) great job – Ricardo Apr 11 '11 at 8:09
12

As you've noticed, this functionality is part of InternalResourceViewResolver and InternalResourceView, whereas the Tiles stuff inherits directly from UrlBasedViewResolver and AbstractUrlBasedView, so you can't make use of it.

Looking at the code, there's no reason why this stuff couldn't have been put into AbstractUrlBasedView. The magic happens in InternalResourceView.getRequestToExpose, and it looks perfectly applicable to AbstractUrlBasedView too.

In the short term, I suggest subclassing UrlBasedViewResolver and TilesView, copying the getRequestToExpose stuff from InternalResourceView. In the longer term, I encourage you to file a issue with SpringSource asking them to move this functionality up the class hierarchy into AbstractUrlBasedView, making it more widely available.

  • I've tried something similar, but it doesn't work. I've subclassed JstlView, overriding initServletContext, checkResource and renderMergedOutputModel. I'll try your idea now. – sinuhepop May 17 '10 at 11:39
  • @Sinuhe: JstlView isn't going to be compatible with Tiles, the mechanisms are fundamentally different. – skaffman May 17 '10 at 12:41
  • Well, your solution worked! While I was doing it, I've noticed my previous idea wasn't correct. I've try it a little more before filing an issue to SpringSource. Thanks! – sinuhepop May 17 '10 at 12:52
  • @skaffman What are you views on 'ServletContextAttributeExporter'? Shouldn't we be using 'ServletContextAttributeExporter' instead of extending UrlBasedViewResolver's functionality? – Sudarshan_SMD Jul 3 '14 at 10:18
13

Have you tried to use ServletContextAttributeExporter in xml configuration file?

<bean
    class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="<bean key here>">
                <ref bean="<bean name here" />
            </entry>
        </map>
    </property>
</bean>
  • 2
    I use this because it is simpler to implement. – msangel Sep 12 '13 at 3:13
  • Certainly This is more elegant and unobtrusive method of adding session beans ! I was about to extend the Spring Classes, your solution saved my several days of work ! THANK YOU ! – Rakesh Waghela Oct 7 '13 at 5:37
  • This is awesome. – user3120173 Mar 11 '14 at 23:09
  • thanks a lot, that was very helpful. – Shaiful Feb 8 '16 at 20:43
1

As of Spring 3.0 there is now a TilesViewResolver.

Convenience subclass of UrlBasedViewResolver that supports TilesView (i.e. Tiles definitions) and custom subclasses of it.

The view class for all views generated by this resolver can be specified via the "viewClass" property. See UrlBasedViewResolver's javadoc for details.

Note: When chaining ViewResolvers, a TilesViewResolver will check for the existence of the specified template resources and only return a non-null View object if the template was actually found.

Since: 3.0 Author: Juergen Hoeller

  • 1
    TilesViewResolver doesnt solve the problem as it subclasses UrlBasedViewResolver without adding exposedContextBeanNames property. The original post and the accepted answer are still the best way to go AFAIK. – azpublic Nov 12 '12 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

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