AngularJS 与 Spring-mvc [英] AngularJS with Spring-mvc

查看:27
本文介绍了AngularJS 与 Spring-mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了 AngularJS.我是一名 Java Web 开发人员.我想将 AngularJS 与 Spring-MVC 框架一起使用.但我需要一些基本的基础(教程)才能开始.

I recently came across AngularJS. I am a java web developer. I want to use AngularJS along with Spring-MVC framework. But I need some basic foundation (tutorial) to start with.

我了解 AngularJS 的基本内容,但如何将其与 Spring-MVC 集成.由于大多数情况下 spring 它自己返回部分 jsp(s),我们使用 jsp-include 添加它们,而 AngularJS 大部分时间都需要 JSON 数据.

I know about the basic stuff of AngularJS, but how to integrate it with Spring-MVC. As most of the time spring it self return partial jsp(s), and we add them using jsp-include, while AngularJS expects JSON data most of the time.

推荐答案

两种情况:

  1. 您的架构是完整的客户端:在这种情况下,集成非常自然.Spring MVC 将您的服务公开为 REST (JSON/XML),并且您的客户端应用程序使用 AngularJS 使用您的 JSON.这里 .war 应用程序 (Spring MVC) 必须部署在 servlet 容器(例如 Tomcat)中,并且您的客户端应用程序可以部署在同一服务器或 HTTP 服务器(如 Nginx 或 Apache)中.

  1. Your architecture is full client-side: In this case the integration is very natural. Spring MVC exposes your service as a REST (JSON/XML) and your client application with AngularJS consumes your JSON. Here the .war application (Spring MVC) must be deployed in a servlet container (e.g. Tomcat) and your client application can be deployed in the same server or in a HTTP server like Nginx or Apache.

您希望在服务器端保留页面生成,并且仅将 AngularJS 用于某些 DOM 操作,因此您的代码必须部署在同一个 .war(在 WEB-INF/文件夹内)中.

You want to keep page generation in the server-side and only use AngularJS for some DOM manipulation so your code must be deployed in the same .war (inside the WEB-INF/ folder).

混合这两种方法并不总是一个好主意.您可以尝试 Thymeleaf 以保持服务器端页面生成,同时拥有模板、可测试性和干净的查看代码.

Mixing the two approaches in not always a good idea. You can try Thymeleaf to stay in server-side page generation and at the same time have templating, testability and clean View code.

这篇关于AngularJS 与 Spring-mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆