我正在寻找在jsp文件中显示Spring属性值的帮助。
我发现一个链接和我的有相同的要求。点击Using spring:eval inside hasRole
我正在使用Spring 2.5
这是我的applicationContext-util.xml文件:
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
<util:properties id="viewPropertyConfigurer" location="classpath:conf/app_config.properties"/>
<context:property-placeholder properties-ref="viewPropertyConfigurer" />
in my menu.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="@viewPropertyConfigurer.getProperty('role.admin')" />
在lib文件夹中,我也有spring-web-2.5.6.jar文件,以确保eval应该在jsp中正常工作。但不确定是什么问题,一旦我添加spring:eval标签jsp不是在加载它抛出
[ERROR,context.JspTilesRequestContext,http-8080-1] -[UID=galips - SessionID=691A896E807850568DF9B0F5356F6CB2] - JSPException while including path '/WEB-INF/jsp/menu.jsp'.
在我的应用程序中,我使用servlet过滤器,也希望这不会是一个问题。
提前感谢您的帮助
据我所知,EvalTag
是在Spring 3 (@since 3.0.1
)中添加的。如果你使用的是spring2.5,那么你没有<spring:eval>
支持。
可能的解决方案:
- 切换到Spring 3+
- 使用自定义处理程序拦截器向您的请求添加附加信息
- 编写自己的标签以从应用程序上下文中提取信息
更新(选项2示例):
public class CommonViewAttributesInterceptor extends HandlerInterceptorAdapter {
private static final String PROPERTIES_ATTR = "properties";
private Properties properties = new Properties();
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
request.setAttribute(PROPERTIES_ATTR, properties);
}
public void setPropertiesSource(Resource resource) throws IOException {
InputStream input = resource.getInputStream();
try {
properties.load(input);
} finally {
input.close();
}
}
}
那么你需要在你的handler映射中配置这个拦截器。