简体   繁体   English

带有嵌套路由的AngularJS和Spring MVC

[英]AngularJS and Spring MVC with nested route

I've done a lot of research about this subject and haven't found a correct or specific answer, so I'm asking it here before opening a jira ticket on the Spring MVC project. 我已经对该主题进行了很多研究,但没有找到正确或具体的答案,因此我在打开Spring MVC项目的jira票证之前先在这里询问。

My application is built with Spring MVC (with spring boot) for the backend and with AngularJS 1.x on front end. 我的应用程序是通过Spring MVC(带有spring boot)(用于后端)以及AngularJS 1.x(位于前端)构建的。 I also have html5mode activated (so, no # used, just plain url like http://podcast.dk.lan/podcasts/123/items/456/player ). 我还激活了html5mode(因此,未使用#,仅使用了简单的网址,例如http://podcast.dk.lan/podcasts/123/items/456/player )。

I have one @Controller route in the back-end redirecting the "/" request to my index.html. 我在后端有一个@Controller路由,将“ /”请求重定向到我的index.html。 All the others routes are handled by @RestController and works with JSON (on "/api" or "/system"). 其他所有路由均由@RestController处理,并与JSON(在“ / api”或“ / system”上)一起使用。

I've searched for the right way to map all the AngularJS routes to the index.html of my application without breaking the other parts of the back-end resolvers (resource for example). 我一直在寻找将所有AngularJS路由映射到应用程序的index.html的正确方法,而又不会破坏后端解析器的其他部分(例如资源)。

I've tried the following elements : 我尝试了以下元素:

@Controller
public class HomeController {   
    @RequestMapping(value = "/{.*}")
    private String home() {
    return "forward:/";
    }
}

It's not working because, if I try to access directly to a nested url on the front-end (like /podcasts/1/items/1 ), the url is not caught by the request mapping regex. 这是行不通的,因为如果我尝试直接访问前端的嵌套URL(例如/ podcasts / 1 / items / 1),则该URL不会被请求映射正则表达式捕获。

@Controller
public class HomeController {   
    @RequestMapping(value = "/**/{.*}")
    private String home() {
    return "forward:/";
    }
}

This configuration lead to a StackOverFlow error because the url will redirect to itself... 此配置导致StackOverFlow错误,因为该网址将重定向到自身。

Recently, in a really good tutorial about Spring-Security and AngularJS, they used the following pattern: 最近,在有关Spring-Security和AngularJS的非常好的教程中,他们使用了以下模式:

@Controller
public class HomeController {   
@RequestMapping(value = "/{[path:[^\\.]*}")
    private String home() {
    return "forward:/";
    }
}

This pattern excludes all resources (css and js) by excluding any url with a . 此模式通过排除带有的任何url来排除所有资源(css和js)。 (dot) in the url. 网址中的(点)。 Unfortunately it's not working with nested routes... returning a 404 error. 不幸的是,它不适用于嵌套路由...返回404错误。

So, my app backend is now linked to my route front-end, because I have to hard-code the AngularJS route use in the front-end to the request mapping value : 因此,我的应用程序后端现在已链接到我的路由前端,因为我必须将前端中的AngularJS路由使用硬编码为请求映射值:

@Controller
public class HomeController {

    @RequestMapping(value = {"", "items", "podcasts", "podcasts/**", "player", "podcast-creation", "download", "stats"})
    private String home() {
        return "forward:/";
    }
}

I think (and I hope) there is a better solution to redirect all "not already mapped url backend mapping" to a specific method like this. 我认为(并且我希望)有一个更好的解决方案,可以将所有“尚未映射的url后端映射”重定向到这样的特定方法。

All the code of my project is hosted on github (if you want to see more code) at davinkevin/Podcast-Server 我项目的所有代码都托管在davinkevin / Podcast-Server上的github(如果您想查看更多代码)上。

Thanks for your help 谢谢你的帮助

I don't know if this is the best solution or not, but it is very simple and worked for us and I believe it should be able to handle your case. 我不知道这是否是最好的解决方案,但是它非常简单并且对我们有用,我相信它应该能够处理您的案件。

The main goal here is to make the backend forward all requests that are supposed to be resolved by the angular routes (in other words, no real resource URIs in the backend) to the root / , so when the browser loads the index and angular app again, angular can resolve the route on the client side. 此处的主要目标是使后端将所有应该由角度路由解决的请求(换句话说,后端中没有实际资源URI)转发到根目录/ ,因此当浏览器加载索引和角度应用程序时同样,角度可以解决客户端的路由。 If you have a well-known and reserved prefix for all angular routes (ie: /fe [short for frontend] ), you can use a request mapping pattern that is like /fe/** that returns forward:/ . 如果您对所有角度路由都有一个众所周知的保留前缀(即/fe [frontend的缩写] ),则可以使用类似/fe/**的请求映射模式,该模式返回forward:/

You just need to make sure that all your angular routes start with /fe/ and there are no static resources in an fe folder or no other request mappings that start with this prefix. 您只需要确保所有角度路由都以/fe/开头,并且fe文件夹中没有静态资源,或者没有其他以该前缀开头的请求映射。

@Controller
public class HomeController { 
    @RequestMapping(value = "/fe/**")
    public String redirect() {
        return "forward:/";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM