Grails重定向以包括尾随正斜杠

mnd*_*mnd 6 grails grails-2.0

我正在使用Grails 2.3.6,并尝试重定向URL以包括结尾的正斜杠,但保留URL的其余部分不变。原因是因为我有动态内容,其中包含图像和附件。我希望这些图像和附件显示为动态内容的子页面,例如:

http://example.com/content
Run Code Online (Sandbox Code Playgroud)

将指向动态生成的内容,没有问题。但是,如果我希望有一个附件,则将包括指向的href链接attachments/file1.pdf。问题在于上面的示例将产生http://example.com/attachments/file1.pdf(注意/content/已被删除)。

我真正想要的是这样的路径:

http://example.com/content/attachments/file1.pdf
Run Code Online (Sandbox Code Playgroud)

我认为最好的解决方法是将内容的URL重定向为包含正斜杠(如果还没有斜杠)。不幸的是,我一直陷于错误或重定向循环。

我在这里尝试了以下建议:Grails UrlMapping重定向以保留DRY,但是当我告诉同一个URL到两个不同的地方时,UrlMappings.groovy的区分不够(唯一的区别是尾随斜杠)

static mappings = {
    "/${content}/"(controller: "application", action: "index")
    "/${content}"(controller: "application", action: "redirectToAddSlash")
}
Run Code Online (Sandbox Code Playgroud)

我还知道Grails 2.3包含UrlMapping重定向,但是提供的示例不包括嵌入式变量。

理想情况下,我想要这样的东西:

static mappings = {
    "/${content}/"(controller: "application", action: "index") <=== Has trailing slash
    "/${content}"(redirect: "/${content}/")                    <=== Redirect to include trailing slash
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的协助!

mnd*_*mnd 4

该解决方案解决了问题的需要,但不是我在问题中寻找的确切解决方案。

使用该<base>元素提供用于所有相对 URL 的基本 href 似乎是最好的解决方法。

对于上面的解决方案,我将使用:

<base href="http://example.com/content">
Run Code Online (Sandbox Code Playgroud)