spring - 哪个 Spring View 解析器与 AngularJS 配合得很好?

标签 spring angularjs spring-mvc

我正在使用 AngularJS 和 Spring MVC 作为 REST 服务提供者和部分 View 提供者编写一个 Web 应用程序(我还使用 Angular-UI-Router 以便可以拥有多个嵌套部分)。我目前对模板语言没有任何用处,因为我计划以 Angular 来做所有事情,但是我尝试过的每个 View 解析器都有某种类型的模板语言,它与 Angular 冲突,要么使应用程序崩溃和/或填充我的日志有错误。

首先,我尝试使用InternalResourceViewResolver,但没有运气,因为它似乎只需要.jsp 文件,不会显示其他任何内容。

然后我尝试使用 Thymeleaf。 Thymeleaf 遵循 XML 标准,这迫使我重写大部分 html 以遵循 xml 要求,在我完成之后,它在 ng-show 指令中遇到 && 时就终止了。所以也没有运气。

然后我尝试了 Velocity。到目前为止,我在速度方面运气最好。它很好地提供了 html 文件,在遇到解析错误时不会停止,并且允许我像 InternalResourceViewResolver 一样提供部分 View 。然而,当遇到以 $ 为前缀的 Angular 变量时,Velocity 会尝试将它们解析为 VTL 变量,并用诸如

之类的消息填充我的日志

velocity - 空引用 [模板“clients/createOrEdit.html”,第 1 行,第 65 列]:无法解析 $invalid。

一切都按其应有的方式运行,但我不是那个只留下错误的人,而且我没有找到禁用 VTL 的方法。

这是我目前使用 View 解析器的经验。

我还有一个想法,使用 mvc:resources 将 .html 文件视为静态资源(它们有点像 Angular 之前那样),但没有任何 View 解析器,我的应用程序无法启动即使我将主layout.html设置为web.xml中的欢迎文件

我的问题是。我应该使用什么作为 View 解析器,以便它与 angularjs 配合良好,以及我是否应该使用 View 解析器?

编辑:我正在尝试使用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/" />

布局.html

<h1>Page with image</h1>
<img src="/static/img/image.jpg"/>

请注意,您必须提及/static/img/image.jpg 而不仅仅是/image.jpg ..同样适用于 css 和 js。

或者

您还可以使用内容协商 View 解析器,如下所示:

 <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)返回合适的 View (基于“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页面

关于spring - 哪个 Spring View 解析器与 AngularJS 配合得很好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838165/

相关文章:

Spring Boot application.properties

java - Web APP中添加Service层,在jsp上显示DB内容

java - Spring Batch - 用于不同分隔文件的 FlatFileItemReader

java - when() 需要一个必须为 'a method call on a mock' 的参数

java - 为什么 JMS MessageListener 中使用的实体管理器不参与 JTA 事务?

javascript - Angular.js 相当于 `rails generate scaffold` ?

java - 登录关系型数据库并扩展日志接口(interface)

javascript - 如何构建操纵 DOM 的 Angular 因子/服务

javascript - Angularjs 使用 ng-repeat 隔离范围指令

javascript - Java-AngularJS http get方法获取带有 ""和转义字符的字符串