【问题标题】:Using spring:eval display property value in jsp在jsp中使用spring:eval显示属性值
【发布时间】:2013-06-06 20:27:51
【问题描述】:

我正在寻求帮助以在 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" />

在我的 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 过滤器,希望它不会成为问题。

提前感谢您的帮助。

【问题讨论】:

  • 请分享配置文件

标签: java spring spring-mvc spring-security


【解决方案1】:

据我所知EvalTag 是在 Spring 3 (@since 3.0.1) 中添加的。如果您使用的是 Spring 2.5,那么您没有 &lt;spring:eval&gt; 支持。

可能的解决方案:

  • 切换到 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();
        }
    }

}

然后你需要在你的处理程序映射中配置这个拦截器。

【讨论】:

  • 感谢nidhin、Pavel Horal 提供宝贵的信息。我用 Spring 3+ 创建了示例项目,它工作正常。我的项目是 2.5+,现在如果我切换到 3.0,那么它需要更改一些包并需要测试整个应用程序。就像 Pavel Horal 所说的创建标签并获取属性值,我可以看到有 2 个选项可以实现它。一世。通过java加载属性。 ii.从 Spring 注入的属性文件中获取属性值。续……
  • 我想使用第二个选项,因为我不想在两个地方提供文件名我检查了 PropertyResourceConfigurer api,没有办法根据键获取值。无论如何我可以通过编程方式获取值如果我遗漏任何东西请告诉我。
  • 很抱歉打扰大家。我得到了如何从 PropertyResourceConfigurer 获取值的链接。这是link to the code
  • 您可以加载属性两次——一次用于PropertyPlaceholderConfigurer,一次在您的自定义处理程序拦截器中。我已经用一个例子更新了我的答案。
  • 嗯...我可能读错了您的 cmets。你要自定义标签解决方案吗?
猜你喜欢
  • 2016-03-25
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 2017-07-23
  • 2018-11-19
  • 1970-01-01
  • 2013-12-08
相关资源
最近更新 更多