解析 JSTL 中的请求 URL
我想根据 JSP 上的 URL 请求显示特定消息。
请求 URL 可以是:
/app/cars/{id}
或者
/app/people/{id}
在我的 messages.properties
上,我得到:
events.action.cars=My car {0} event
events.action.people=My person {1} event
最后,在我的 JSP 页面上,我想要以下代码:
<spring:message code="events.${element.cause}.${?????}"
arguments="${element.param['0']},${element.param['1']}"/>
我需要帮助弄清楚我可以使用哪个表达式来解析请求URL并获取ID前的单词。
I want to display a specific message based on the URL request on a JSP.
the request URL can be:
/app/cars/{id}
OR
/app/people/{id}
On my messages.properties
I've got:
events.action.cars=My car {0} event
events.action.people=My person {1} event
Finally, on my JSP page I want to have the following code:
<spring:message code="events.${element.cause}.${?????}"
arguments="${element.param['0']},${element.param['1']}"/>
I need help figuring out which expression I could use to parse the request URL and obtain the word before the ID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以按如下方式访问 JSTL 中的请求 URI(实际上:EL):(
因此返回
HttpServletRequest#getRequestURI()
)然后,要确定它,您必须使用 JSTL 函数标签库。它提供了多种字符串操作方法,例如
split()
、indexOf()
、substringAfter()
等。不,没有人支持正则表达式。解析一下就可以了启动示例:
并将其用作
${id}
。You can access the request URI in JSTL (actually: EL) as follows:
(which thus returns
HttpServletRequest#getRequestURI()
)Then, to determine it, you'll have to play a bit round with JSTL functions taglib. It offers several string manipulation methods like
split()
,indexOf()
,substringAfter()
, etc. No, no one supports regex. Just parse it.Kickoff example:
And use it as
${id}
.将把
cars
或people
放入反向引用\1
中,具体取决于匹配情况,以及反向引用中最后一个斜杠左边的内容\2
。will put
cars
orpeople
in backreference\1
, depending on the match, and whatever is left right of the last slash in backreference\2
.到目前为止,我的解决方案是拥有一个与正则表达式 ".?/jsp/(\w+)/..jsp" 匹配的 RequestUtils 类并返回 group(1) 。
在我的 Jsp 中我得到:
这当然成功了。但 JSP 中最好不要有任何 Java 代码。
My solution so far is to have a RequestUtils class that match the regex ".?/jsp/(\w+)/..jsp" and return the group(1).
in my Jsp I got:
this of course did the trick. But still it would be better not to have any Java code within the JSP.
如果我理解正确的话,我认为你需要做这样的事情:
如你所见,这里的ownerId是由Spring MVC从URL中读取的。之后,您只需将变量放入模型映射中,以便可以在 JSP 中使用它。
If I understand you correctly, I think you need to do something like this:
As you can see, here the ownerId is read from the URL by Spring MVC. After that, you simply put the variable in the Model map so you can use it in your JSP.