Accessing Spring beans from a Tiles view (JSP)

前端 未结 3 1323
逝去的感伤
逝去的感伤 2020-12-03 01:33

In Spring MVC I can access my beans in JSP using JstlView\'s exposedContextBeanNames (or exposeContextBeansAsAttributes). For example, then, in my JSP I can write (${propert

相关标签:
3条回答
  • 2020-12-03 02:06

    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>
    
    0 讨论(0)
  • 2020-12-03 02:13

    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

    0 讨论(0)
  • 2020-12-03 02:15

    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.

    0 讨论(0)
提交回复
热议问题