简体   繁体   English

Spring MVC RedirectView到带参数的AngularJS URL

[英]Spring MVC RedirectView to AngularJS URL with parameters

I'm trying to redirect my users to an AngularJS style URL: 我正在尝试将用户重定向到AngularJS样式的URL:

http://somedomain.com/#/oauth/authorize

And I have some parameters I'm adding to the URL via my model. 我有一些参数要通过我的模型添加到URL。 So far, so standard. 到目前为止,如此标准。 Right up until Spring MVC takes a dark and sinister turn inside of RedirectView at line 400. If you click that link you'll see the comment: 一直到Spring MVC在第400行的RedirectView内进行黑暗而险恶的转弯。如果单击该链接,则会看到注释:

// Append anchor fragment, if any, to end of URL.

No! 没有! This is not good for AngularJS! 这对AngularJS不利! That would make my normally beautiful redirect: 那将使我通常正常的重定向:

http://somedomain.com/#/oauth/authorize?query=params

look like: 看起来像:

http://somedomain.com/?query=params#/oauth/authorize

This is bad juju! 这是坏枣!

So I have two questions: 所以我有两个问题:

  1. What purpose does this arrangement of anchor and query params serve. 锚定和查询参数的这种布置目的是什么?
  2. How do I circumvent this devilish machinery? 我该如何规避这种恶魔般的机器?

Thanks for any help! 谢谢你的帮助!

I feel like I need to take a shower after doing this, but this is a possible solution. 我觉得这样做后需要洗个澡,但这是一个可能的解决方案。

Place this in your main script (ie app.js ) immediately before creating the module in which you configure your $routeProvider . 在创建用于配置$routeProvider的模块之前,将其立即放置在您的主脚本(即app.js )中。

var urlLoaded = window.location.href;
var qIndex = urlLoaded.indexOf('?');
var hashIndex = urlLoaded.indexOf('#');

if (qIndex !== -1 && hashIndex !== -1 && qIndex < hashIndex) {

    var correctedPath = urlLoaded.substring(hashIndex);
    var correctedParams = urlLoaded.substring(qIndex, hashIndex);
    var base = urlLoaded.substring(0, qIndex);
    var correctedUrl = base + correctedPath + correctedParams;

    window.location.href = correctedUrl;
}
  • If you are in the early stages, I would look into using HTML5 pushState. 如果您还处于初期阶段,我会考虑使用HTML5 pushState。 This way you can use anchors like they were meant to be used, and you url's will look cleaner. 这样,您可以像使用锚一样使用锚,这样您的网址看起来会更干净。 There are browser limitations to this, and other considerations, but getting it in correctly might save you time down the line from running into this again. 浏览器对此有局限性,还有其他注意事项,但是正确安装它可能会使您节省时间,以免再次遇到此问题。

  • You can extend this class, override the method, and fix it to be smarter than it currently is. 您可以扩展此类,重写方法,并将其修复为比当前更聪明。 Register it with spring (it might take a couple of overrides, look at ViewControllerRegistry), and if you're feeling nice, submit a Pull Request and see if they want it. 在spring上注册它(可能需要重写,查看ViewControllerRegistry),如果感觉不错,请提交Pull Request并查看他们是否想要它。

  • Beat that URL back into submission in angular as @hartz1989 has suggested 按照@ hartz1989的建议将URL打回呈角度提交状态

  • Instead of doing a redirect, you could respond back with a query parameter that has the redirected url, and add a handler in angular that understands this and does the "redirect". 除了进行重定向之外,您还可以使用具有重定向的url的查询参数进行响应,并添加一个可以理解此内容并执行“重定向”的Angular处理程序。 I don't really like this idea, but it is another idea. 我不太喜欢这个主意,但这是另一个主意。

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

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