哪个弹簧视图解析器与angularjs一起玩得很好?
我正在使用
angularjs和spring mvc编写一个webapp作为REST服务提供者和一个部分视图提供者(我也使用angular-ui-router,这样我就可以有多个嵌套的部分).我目前对模板语言没有任何用处,因为我计划用角度做所有事情,但是我尝试过的每一个视图解析器都有某种类型的模板语言与angular冲突并且崩溃应用程序和/或填充我的日志有错误.
首先我尝试使用InternalResourceViewResolver,但没有运气,因为它似乎只需要.jsp文件,并且不会显示任何其他内容. 然后我尝试使用Thymeleaf. Thymeleaf遵循XML标准,这迫使我重写我的大多数html以遵循xml要求,并且在我完成之后它在遇到&&&在ng-show指令中.所以也没有运气. 然后我尝试了Velocity.到目前为止,我对速度感到非常幸运.它很好地提供了html文件,在遇到解析错误时不会停止,并允许我以与InternalResourceViewResolver相同的方式提供部分视图.然而,在遇到以$Velocity为前缀的角度变量时,会尝试将它们解析为VTL变量,并使用以下消息填充我的日志 velocity – 空引用[模板’clients / createOrEdit.html’,第1行,第65列]:$invalid无法解析. 一切都在继续工作,但我不是那个只留下错误的人,而且我找不到禁用VTL的方法. 这是我目前使用视图解析器的经验. 我还有一个想法,即使用mvc:resources将.html文件视为静态资源(它们之前有点像魔术之前),但是没有任何视图解析器我的应用程序无法启动,即使我将主layout.html设置为是web.xml中的welcome-file 我的问题是.我应该使用什么作为视图解析器,以便它与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页面 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |