有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在寻找在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" />

在我的菜单上。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过滤器,希望这不会成为一个问题

提前谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

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

    可能的解决办法:

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

    然后,您需要在处理程序映射中配置此拦截器