哪个Spring View解析器可以与angularjs配合使用?
问题内容:
我正在使用angularjs和spring mvc作为REST服务提供程序和部分视图提供程序编写一个webapp(我也使用angular-ui-
router,以便可以有多个嵌套的部分)。我目前对模板语言没有任何用处,因为我打算按角度进行所有操作,但是我尝试过的每个单一视图解析器都有某种类型的模板语言,与角度冲突,或者使应用程序崩溃和/或填充我的日志有错误。
首先,我尝试使用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。
这就是我目前在视图解析器上的经验。
我也有一个想法使用.html文件将其视为静态资源(它们在mvc:resources
某种角度之前就算是静态的)在没有任何视图解析器的情况下,即使我将main
layout.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(调度程序Servlet中的contextconfig)
<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使用静态资源(html,css,img,js),请使用类似于以下内容的目录结构:
src/
package/
LayoutController.java
WebContent/
WEB-INF/
static/
html/
layout.html
images/
image.jpg
css/
test.css
js/
main.js
web.xml
springmvc-servlet.xml
@Controller
public class LayoutController {
@RequestMapping("/staticPage")
public String getIndexPage() {
return "layout.htm";
} }
<!-- in spring config file -->
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
layout.html
<h1>Page with image</h1>
<img src="/static/img/image.jpg"/>
请注意,您必须提及/static/img/image.jpg,而不仅仅是/image.jpg ..同样适用于CSS和JS。
要么
您还可以使用 内容协商视图解析器 ,如下所示:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="rss" value="application/rss+xml" />
<entry key="html" value="text/html"/>
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
Spring MVC将使用“ ContentNegotiatingViewResolver”(order = 1)返回合适的视图(基于在“
mediaTypes ”属性中声明的文件扩展名),如果不匹配,则使用“ InternalResourceViewResolver”(order =
2)返回默认的JSP页面。
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
现在,从您的jsp中,您也可以重定向到静态html页面
@Controller
public class LayoutController {
@RequestMapping("/index")
public String getIndexPage() {
return "index";
}
}
index.jsp
<form:form method="GET" action="/static/html/layout.html">
现在尝试通过http://yourapp.com/ / index
访问您的服务,显示上述表单操作,它将显示layout.html
单击一个按钮或在jsp页面中提交以调用layout.html页面