[英]Slashes in property in Url string Java Spring MVC
我正在为我的工作开发一个 Java Spring MVC 应用程序。 当 URL 中有斜杠时,我们总是会遇到问题。 我们正在使用 Java 8 和 Tomcat 7。在“当前页面”的 JSP 中我们有这个 href:
<td style="vertical-align: middle;"><a id="column" style="color: #EB9316" href="${pageContext.request.contextPath}/user/other-page/${c.property}/${dto.year}">${c.property}</a></td>
这里我们将 object 的属性和年份作为参数传递。 在“其他页面”中,我们获取属性和年份并过滤数据库上的 object c。 问题是 property 是一个字符串,有时名称中可能带有斜杠,例如“thing/otherthing”,我们必须同时使用年份和 property 才能在其他页面中进行过滤。 年份取自页面的上下文,不会出现问题。
按照“其他页面”Java controller 中的代码:
@GetMapping("/user/other-page/{property}/{year}")
public String homeUserOtherPageFilter(@PathVariable String property, @PathVariable Integer year, RedirectAttributes attributes) {
property= URLDecoder.decode(property, "UTF-8");
attributes.addFlashAttribute("property", property);
attributes.addFlashAttribute("year", year);
return "redirect:/user/other-page";
}
使用名称中的斜杠我得到错误 404。我尝试使用“当前页面”java Controller 中的此代码对斜杠进行编码。
try {
for (chapter chapter: chapters) {
chapter.setProperty(URLEncoder.encode(chapter.getProperty(), "UTF-8"));
}
} catch (UnsupportedEncodingException e){
logger.debug("Url encode failed ", e);
}
但这仍然不起作用,即使使用 URL 编码的 Tomcat 给我错误 404,甚至没有到达其他页面 controller 进行解码。 如果我在“current-page”中的 2020 页面上单击“thing/otherthing”链接,则准确的错误是:
请求的资源 [/application/user/other-page/2020/thing%2Fotherthing] 不可用
当然,对于没有斜线的属性,一切都很好。 我究竟做错了什么?
编辑:我解决了这个问题。 出于某种奇怪的原因,Tomcat 不喜欢将“/”转换为“%2F”,但出于某种我不知道的原因,接受“/”的编码为“%252F”,这是通过对参数进行两次编码给出的。 所以我只需要将当前页面 Java controller 中的代码更改为
try {
for (chapter chapter: chapters) {
chapter.setProperty(URLEncoder.encode(URLEncoder.encode(chapter.getProperty(), "UTF-8"), "UTF-8"));
}
} catch (UnsupportedEncodingException e){
logger.debug("Url encode failed ", e);
}
现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.