相关疑难解决方法(0)

如何以编程方式解析Spring中的属性占位符

我目前正在开发基于Spring 3.1.0.M1的基于注释的Web应用程序,并且我在应用程序的一个特定位置解析属性占位符时遇到问题.

这是故事.

1)在我的Web应用程序上下文(由DispatcherServlet加载)中,我有

MVC-config.xml文件:

<!-- Handles HTTP GET requests for /resources/version/**  -->
<resources mapping="/${app.resources.path}/**" location="/static/" cache-period="31556926"/> 

...

<!-- Web properties -->
<context:property-placeholder location="
    classpath:app.properties
    "/>
Run Code Online (Sandbox Code Playgroud)

2)在app.properties内部,有2个属性,其中包括:

app.properties:

# Properties provided (filtered) by Maven itself
app.version: 0.1-SNAPSHOT
...

# Static resources mapping
app.resources.path: resources/${app.version}
Run Code Online (Sandbox Code Playgroud)

3)我在JSP 2.1模板中有一个JSP自定义标记.此标记负责完整的资源路径构建,具体取决于环境设置,应用程序版本,弹簧主题选择等.自定义标记类扩展spring:url实现类,因此它可能被视为通常的url标记,但具有关于正确路径的一些额外知识.

我的问题是我无法在JSP自定义标记实现中正确解析$ {app.resources.path}.JSP自定义标记由servlet容器管理,而不是Spring,因此不参与DI.所以我不能只使用通常的@Value("$ {app.resources.path}")并由Spring自动解决它.

我所有的都是Web应用程序上下文实例,所以我必须以编程方式解析我的属性.

到目前为止我试过:

ResourceTag.java:

// returns null
PropertyResolver resolver = getRequestContext().getWebApplicationContext().getBean(PropertyResolver.class);
resolver.getProperty("app.resources.path");


// returns null, its the same web context instance (as expected)
PropertyResolver resolver2 = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean(PropertyResolver.class);
resolver2.getProperty("app.resources.path");


// throws NPE, resolver3 …
Run Code Online (Sandbox Code Playgroud)

spring jsp dependency-injection jsp-tags custom-tag

37
推荐指数
4
解决办法
4万
查看次数

标签 统计

custom-tag ×1

dependency-injection ×1

jsp ×1

jsp-tags ×1

spring ×1