如何在SpringMVC中配置emberjs路由? [英] How to configure emberjs routes in SpringMVC?

查看:155
本文介绍了如何在SpringMVC中配置emberjs路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Ember前端和Spring Boot后端。
当Ember在端口4200上独立运行,并且8080上的Spring Boot后端运行时,一切正常。但是,这种情况在生产环境中有些不寻常,不仅因为CORS问题。因为Emer应用程序的构建时间(!)已经在后端知道了URL,因为它已被集成到编译的ember应用程序中。这对于许多项目是不可能的。因此,我们想在Spring Boot后端集成前端Ember应用程序,这通常是例如。 SPA与AngularJS。
Ember应用程序(从/ dist)因此被复制到src / main / resource / static。调整rootURL和API.host与Ember应用程序,工作得很好。
现在问题出现在浏览器中手动重新加载URL时。这样的URL现在是Ember路由。 http请求到达不了解路由的Spring Boot后端,我们收到404错误。



SpringMVC(作为Spring Boot后端的一部分) )答复这样的路由的httpRequest,以便Ember应用程序继续工作并处理请求?

  HTML页面请求通过浏览器)
http://host/springBootAppContext/index.html => src / main / resource / static / index.html(ember app)

REST API请求(由Ember App)
http:// host / springBootAppContext / users => RESTController映射为/ users

Ember Routing(by Ember App)
http:// host / springBootAppContext / user-list => ???

你不能提供一个普通的Spring MVC @Controller类,因为ModelView响应是以user- list.html或类似的不存在

解决方案

经过不同解决方案的测试后,我想出了一个非常简单的解决方案。
为ember应用程序定义的每个路由提供一个正常的非REST控制器,并提供请求映射。每个这样的请求必须用ember应用程序起始页的视图名称(大多数情况下是index.html)来回答。浏览器加载html页面并启动包含ember应用程序。 ember应用程序检测到路由(因为路由是http请求的一部分),并根据所有指定的路由配置(模板,模型,控制器)进行工作。



No 404 NotFound any more。

  @Controller 
public class EmberRouteController {

public static final String INDEX =index.html;

@RequestMapping(value =/ ember-route-1,method = RequestMethod.GET)
public String emberRoute1(){
return INDEX;
}

@RequestMapping(value =/ ember-route-2,method = RequestMethod.GET)
public String emberRoute2(){
return INDEX;
}

}


We have an Ember frontend and Spring Boot backend. When Ember runs standalone on port 4200 and the Spring Boot backend on 8080, then everything works. But this scenario is somewhat unusual for production environments, not only because of CORS problem. The URL of the backend must be known already on build time (!) of the Ember application, because it's integrated within the compiled ember app. This is not possible for many projects. Therefore, we want to integrate the frontend Ember App in the Spring Boot backend, which is usual for e.g. SPA with AngularJS. The Ember app (from /dist) is thus copied to src/main/resource/static. After adjusting the rootURL and API.host with the Ember app that works very well. The problem arises now, when a manual reload for an URL is made in the browser. Such a URL is now an Ember route. The http-request arrives at the Spring Boot backend which don't knows the route and we got a 404 error.

How should SpringMVC (as part of the Spring Boot backend) answers the httpRequest for such a route, so that the Ember app continue their work and handle the request ?

HTML Page request (by browser)
http://host/springBootAppContext/index.html => src/main/resource/static/index.html (ember app) 

REST API request (by Ember App)
http://host/springBootAppContext/users => RESTController mapped for /users

Ember Routing (by Ember App)
http://host/springBootAppContext/user-list => ???

You can't provide a normal Spring MVC @Controller class because the ModelView response is interpretet as user-list.html or similar which doesn't exist

解决方案

after tests with different solutions I came up with a really simple one. Provide a normal, non-REST controller with request mappings for every route defined by the ember app. Every such request have to be answered with the view name of the ember app start page (index.html in most cases). The browser loads that html page and starts the containing ember app. The ember app detects the route (because the route was part of the http request) and work according all specified route configurations (template, model, controller).

No 404 NotFound any more.

@Controller
public class EmberRouteController {

public static final String INDEX = "index.html";

@RequestMapping(value = "/ember-route-1", method = RequestMethod.GET)
public String emberRoute1() {
    return INDEX;
}

@RequestMapping(value = "/ember-route-2", method = RequestMethod.GET)
public String emberRoute2() {
    return INDEX;
}

}

这篇关于如何在SpringMVC中配置emberjs路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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