java 在jsp中使用spring:eval显示属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16971565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:35:18  来源:igfitidea点击:

Using spring:eval display property value in jsp

javaspringspring-mvcspring-security

提问by sats

I am looking for help to display the Spring property value in a jsp file.

我正在寻找帮助以在 jsp 文件中显示 Spring 属性值。

I found one link which is having the same requirement of mine. Click Using spring:eval inside hasRole

我找到了一个与我有相同要求的链接。单击在 hasRole 中使用 spring:eval

I am using Spring 2.5

我正在使用Spring 2.5

This is my applicationContext-util.xml file:

这是我的 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

在我的 menu.jsp 中

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="@viewPropertyConfigurer.getProperty('role.admin')" />

in lib folder I also have spring-web-2.5.6.jar file to make sure the eval should work fine in jsp. But not sure what is the issue once i add spring:eval tag the jsp is not at all loading it throws

在 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'.

In my application I am using servlet filter as well hope it will not be an issue.

在我的应用程序中,我也使用 servlet 过滤器,希望它不会成为问题。

Thanks for your help in advance.

提前感谢您的帮助。

采纳答案by Pavel Horal

As far as I know EvalTagwas added in Spring 3 (@since 3.0.1). If you are using Spring 2.5, then you don't have <spring:eval>support.

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

Possible solutions:

可能的解决方案:

  • switch to Spring 3+
  • use a custom handler interceptor to add additional information to your request
  • write your own tag to pull up information from application context
  • 切换到 Spring 3+
  • 使用自定义处理程序拦截器向您的请求添加其他信息
  • 编写您自己的标签以从应用程序上下文中提取信息


Update (option 2 example):

更新(选项 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();
        }
    }

}

Then you need to configure this interceptor within your handler mapping.

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