微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

哪个弹簧视图解析器与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页面

原文地址:https://www.jb51.cc/angularjs/142154.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐