Using spring:eval display property value in jsp

[亡魂溺海] 提交于 2021-02-07 04:00:50

问题


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

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

I am using Spring 2.5

This is my applicationContext-util.xml file:

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')" />

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

[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.

Thanks for your help in advance.


回答1:


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

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

Update (option 2 example):

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.



来源:https://stackoverflow.com/questions/16971565/using-springeval-display-property-value-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!