java Spring MVC:相对 URL 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4045149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:31:27  来源:igfitidea点击:

Spring MVC: Relative URL problems

javaspringjspspring-mvcjstl

提问by Corey

I have a controller bound the URL: "/ruleManagement".

我有一个控制器绑定了 URL:“/ruleManagement”。

Inside my JSP, I have a form that forwards (on submit) to "ruleManagement/save" url. When there are errors with the input fields, I want it to return back the original form View. This is where the problem starts...

在我的 JSP 中,我有一个表单转发(提交时)到“ruleManagement/save”url。当输入字段出现错误时,我希望它返回原始表单视图。这就是问题开始的地方......

Problem 1) Now that the URL is "/ruleManagement/save", my form submit now points to "/ruleManagement/ruleManagement/save".

问题 1) 现在 URL 是“/ruleManagement/save”,我的表单提交现在指向“/ruleManagement/ruleManagement/save”。

Problem 2) I tried using spring:url tag to generate the absolute paths for me, which usually works great. But when I put a spring:url tag inside of a tag, the spring:url tag does not get parsed correctly.

问题 2)我尝试使用 spring:url 标记为我生成绝对路径,这通常效果很好。但是,当我将 spring:url 标记放入标记中时, spring:url 标记无法正确解析。

<form:form action="<spring:url value='/ruleManagement/save' ...>" method="post">

When I analyze the DOM after the page loads, my form tag looks something like:

当我在页面加载后分析 DOM 时,我的表单标签看起来像:

<form action='<spring:url value="/ruleManagement/save" />' ... >

If I don't use the spring:url tag, and instead use just "/ruleManagement/save", the url generated excludes my application name in the url, which is also wrong.

如果我不使用 spring:url 标签,而只使用“/ruleManagement/save”,则生成的 url 在 url 中排除了我的应用程序名称,这也是错误的。

How do I generate a consistent URL pattern across all Views regardless of path? If the answer is "using spring:url", how do I get that content inside a form:form tag?

无论路径如何,如何在所有视图中生成一致的 URL 模式?如果答案是“使用 spring:url”,我如何在 form:form 标签中获取该内容?

回答by axtavt

Custom tags in JSP can't be used in attributes of other custom tags, so you need to store intermediate result in a request attribute (using varto redirect output of the tag to the request attribute is a common idiom supported by many tags):

JSP中的自定义标签不能用在其他自定义标签的属性中,所以需要将中间结果存储在一个请求属性中(var用于将标签的输出重定向到请求属性是很多标签都支持的常用习惯用法):

<spring:url var = "action" value='/ruleManagement/save' ... />
<form:form action="${action}" method="post"> 

回答by Kaleb F.

I too would love to be able to generate a consistent URL path across all Views! Is this possible with <spring:url .../>.

我也希望能够在所有视图中生成一致的 URL 路径!这可能与<spring:url .../>.

To answer your second question & tacking on to axtavt's answer, embed the <spring:url ... />into the form action after adding the property htmlEscape="true"

要回答您的第二个问题并继续 axtavt 的回答,请<spring:url ... />在添加属性后将其嵌入到表单操作中htmlEscape="true"

Example: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">

例子: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">