从磁贴视图 (JSP) 访问 Spring Bean
在Spring MVC中,我可以使用JstlView的poseedContextBeanNames(或exposeContextBeansAsAttributes)在JSP中访问我的bean。例如,在我的JSP中,我可以写(${properties.myProperty)。但是,当同一 JSP 是磁贴视图的一部分时,这些属性是不可访问的。是否可以正确配置磁贴或以其他方式访问这些属性?
我使用的是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;
}
}
磁贴显示BeansView.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);
}
}
弹簧配置:
<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。我已经做了一些测试,但也许有必要做更多。