【发布时间】:2014-09-10 08:50:49
【问题描述】:
我正在编写一个使用 angularjs 和 spring mvc 作为 REST 服务提供者和部分视图提供者的 web 应用程序(我也在使用 angular-ui-router 以便我可以有多个嵌套的部分)。我目前对模板语言没有任何用处,因为我打算用 Angular 做所有事情,但是我尝试过的每一个视图解析器都有某种类型的模板语言,它与 Angular 冲突,或者导致应用程序崩溃和/或填充我的日志有错误。
首先我尝试使用 InternalResourceViewResolver 但没有运气,因为它似乎只需要 .jsp 文件并且不会显示其他任何内容。
然后我尝试使用 Thymeleaf。 Thymeleaf 遵循 XML 标准,这迫使我重写我的大部分 html 以遵循 xml 要求,并且在我完成之后,它在 ng-show 指令中遇到 && 时死亡。所以也没有运气。
然后我尝试了 Velocity。到目前为止,我在速度方面最幸运。它很好地提供了 html 文件,在遇到解析错误时不会停止,并允许我以与 InternalResourceViewResolver 相同的方式提供部分视图。然而,在遇到以$为前缀的角度变量时,Velocity 会尝试将它们解析为 VTL 变量,并在我的日志中填充类似
velocity - Null reference [template 'clients/createOrEdit.html', line 1, column 65] : $invalid cannot be resolved.
一切都按应有的方式正常运行,但我不是那种只会留下错误的人,而且我找不到禁用 VTL 的方法。
这是我目前使用视图解析器的经验。
我也有一个想法,使用mvc:resources 将 .html 文件视为静态资源(它们有点在 Angular 之前是魔法),但没有任何视图解析器,即使我设置了主布局,我的应用程序也无法启动.html 成为 web.xml 中的欢迎文件
我的问题是。我应该使用什么作为视图解析器,以便它与 angularjs 配合得很好,如果我什至应该使用视图解析器?
编辑:我正在尝试使用ContentNegotiatingViewResolver,我得到:
DEBUG ContentNegotiatingViewResolver - Requested media types are [text/html] based on Accept header types and producible media types [*/*])
DEBUG ContentNegotiatingViewResolver - No acceptable view found; returning null
DEBUG DispatcherServlet - Could not complete request
javax.servlet.ServletException: Could not resolve view with name 'layout.html' in servlet with name 'springDispatcherServlet'
webapp-config.xml(dispatcher servlet 中的上下文配置)
<mvc:annotation-driven />
<!-- Resources -->
<mvc:resources location="/libs/" mapping="/libs/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- Angular application data -->
<mvc:resources location="/WEB-INF/appjs/" mapping="/appjs/**" />
<!-- View locations -->
<mvc:resources location="/WEB-INF/html/" mapping="/**"/>
<!-- Controllers -->
<context:component-scan base-package="com.mrplow.controller" />
<!-- Views -->
<util:map id="contentMediaTypes">
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
</util:map>
<!-- <util:list id="defaultViews"> -->
<!-- <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> -->
<!-- </util:list> -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="1"
p:ignoreAcceptHeader="false"
p:defaultContentType="text/html"
p:mediaTypes-ref="contentMediaTypes" />
LayoutController.java
@Controller
@RequestMapping("/")
public class LayoutController {
@RequestMapping
public String getIndexPage() {
return "layout";
}
}
【问题讨论】:
标签: spring angularjs spring-mvc