在Spring MVC中,我可以使用JstlView的exposedContextBeanNames(或exposeContextBeansAsAttributes)在JSP中访问我的bean。例如,然后,在我的JSP中我可以编写($ {properties.myProperty)。但是,当相同的JSP是切片视图的一部分时,无法访问这些属性。是否可以正确配置Tiles或以其他方式访问这些属性?
我正在使用Spring MVC 3.0.2和Tiles 2.2.1。这是我的一些配置:
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="order" value="1"/>
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="order" value="2"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="exposedContextBeanNames">
<list><value>properties</value></list>
</property>
</bean>
编辑:我已经实施了Skaffman的解决方案。
TilesExposingBeansViewResolver.java:
package es.kcsolutions.util.spring.servlet.view;
import org.springframework.web.servlet.view.*;
public class TilesExposingBeansViewResolver extends UrlBasedViewResolver {
private Boolean exposeContextBeansAsAttributes;
private String[] exposedContextBeanNames;
public void setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes) {
this.exposeContextBeansAsAttributes = exposeContextBeansAsAttributes;
}
public void setExposedContextBeanNames(String[] exposedContextBeanNames) {
this.exposedContextBeanNames = exposedContextBeanNames;
}
@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
AbstractUrlBasedView superView = super.buildView(viewName);
if (superView instanceof TilesExposingBeansView) {
TilesExposingBeansView view = (TilesExposingBeansView) superView;
if (this.exposeContextBeansAsAttributes != null) view.setExposeContextBeansAsAttributes(this.exposeContextBeansAsAttributes);
if (this.exposedContextBeanNames != null) view.setExposedContextBeanNames(this.exposedContextBeanNames);
}
return superView;
}
}
TilesExposingBeansView.java:
package es.kcsolutions.util.spring.servlet.view;
import java.util.*;
import javax.servlet.http.*;
import org.springframework.web.context.support.ContextExposingHttpServletRequest;
import org.springframework.web.servlet.view.tiles2.TilesView;
public class TilesExposingBeansView extends TilesView {
private boolean exposeContextBeansAsAttributes = false;
private Set<String> exposedContextBeanNames;
public void setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes) {
this.exposeContextBeansAsAttributes = exposeContextBeansAsAttributes;
}
public void setExposedContextBeanNames(String[] exposedContextBeanNames) {
this.exposedContextBeanNames = new HashSet<String>(Arrays.asList(exposedContextBeanNames));
}
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest) {
if (this.exposeContextBeansAsAttributes || this.exposedContextBeanNames != null)
return new ContextExposingHttpServletRequest(originalRequest, getWebApplicationContext(), this.exposedContextBeanNames);
return originalRequest;
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest requestToExpose = getRequestToExpose(request);
exposeModelAsRequestAttributes(model, requestToExpose);
super.renderMergedOutputModel(model, requestToExpose, response);
}
}
Spring配置:
<bean id="tilesViewResolver" class="es.kcsolutions.util.spring.servlet.view.TilesExposingBeansViewResolver">
<property name="order" value="1"/>
<property name="viewClass" value="es.kcsolutions.util.spring.servlet.view.TilesExposingBeansView"/>
<property name="exposedContextBeanNames">
<list><value>properties</value></list>
</property>
</bean>
如果您遇到问题,请查看TilesExposingBeansView.renderMergedOutputModel。我做了一些测试,但也许有必要做更多的事情。
答案 0 :(得分:13)
您是否尝试在xml配置文件中使用 ServletContextAttributeExporter ?
<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>
答案 1 :(得分:12)
正如您所注意到的,此功能属于InternalResourceViewResolver
和InternalResourceView
,而Tiles内容直接来自UrlBasedViewResolver
和AbstractUrlBasedView
,因此您无法利用它。
查看代码,没有理由将这些内容放入AbstractUrlBasedView
。魔术发生在InternalResourceView.getRequestToExpose
,它看起来也完全适用于AbstractUrlBasedView
。
在短期内,我建议继承UrlBasedViewResolver
和TilesView
,从getRequestToExpose
复制InternalResourceView
内容。从长远来看,我建议您向SpringSource提出问题,要求他们将此功能在类层次结构中移至AbstractUrlBasedView
,以便更广泛地使用。
答案 2 :(得分:1)
从Spring 3.0开始,现在有一个TilesViewResolver。
支持TilesView的UrlBasedViewResolver的便捷子类 (即Tiles定义)及其自定义子类。
此解析程序生成的所有视图的视图类都可以 通过“viewClass”属性指定。请参阅UrlBasedViewResolver javadoc了解详情。
注意:链接ViewResolvers时,TilesViewResolver将检查 指定模板资源的存在且只返回一个 如果实际找到模板,则为非null View对象。
自: 3.0作者:Juergen Hoeller