我目前正在开发基于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 MVC 3.1.M1,JSP 2.1构建一个Web应用程序(没有Tiles,我使用普通的JSP标记文件来构建我的布局).
基本上,我的页面是使用一些常见部分的布局构建的 - 页眉,页脚,横幅,菜单等.这些部分大多是动态的,即包含当前用户的相关信息.
JSP没有"组件"概念,所以我无法在一些地方定义我的模板的一部分及其支持java代码,耦合在一起.在我的@Controllers中,我必须完全填充我的模型,包括页眉,页脚,菜单和其他内容的数据.我真正想要做的是避免这种代码重复.具有一些通用模型填充方法的抽象BaseController类也看起来不太好.
JSP和Spring MVC是经常一起使用的,所以我希望在这个主题上存在一些最佳实践.让我们讨论一下.