[英]How to get JSTL Tag like ${Some} in jsp page but Some is not a value
[英]how to get some value from url using jstl
我有兩種形式的那些具有same-url
的action
,下面的表格是頁http://www.domain.com/pre-foo-url
,這是
<form:form commandName="some" class="form" action="/app/same-url">
<form:input path="title"/>
<input type="hidden" name="redirect" value="foo"/>
<button>OK</button>
</form:form>
另一個表格在http://www.domain.com/bar/{id}
<form:form commandName="some" class="form" action="/app/same-url">
<form:input path="tile"/>
<input type="hidden" name="redirect" value="bar"/>
<button>OK</button>
</form:form>
我的控制器中有兩種方法,一種用於決定重定向到
@RequestMapping(value = "/same-url", method = RequestMethod.POST)
public String handleRedirect(@RequestParam("redirect") String redirect) {
if (redirect.equals("foo")) {
return "redirect:/foo";
} else {
return "redirect:/bar/{id}"; // this {id} must get the value from http://www.domain.com/bar/{id}<-- Here
}
}
從return "redirect:/bar/{id}";
獲取 id 值的其他方法return "redirect:/bar/{id}";
並轉到/bar/{id}
請求映射
@RequestMapping(value = "/bar/{id}", method = RequestMethod.GET)
public String handleBar(@PathVariable Integer id) {
// some logic here
return "go-any-where";
}
現在如何從http://www.domain.com/bar/{id}
獲取值並在我將其redirect:/bar/{id}
到redirect:/bar/{id}
,
我有一個滿足您需求的解決方案,首先我必須指出您的需求,然后我會寫下我的答案。
第一的:
- 您需要從
http://www.domain.com/bar/{id}
獲取/{id}
,這意味着您想要獲取 url最后一部分的值。
您可以在頁面http://www.domain.com/bar/{id}
上添加以下代碼來獲得該值
<c:set var="currentPage" value="${requestScope['javax.servlet.forward.request_uri']}"/> <!--This will give you the path to current page eg- http://www.domain.com/bar/360 -->
<c:set var="splitURI" value="${fn:split(currentPage, '/')}"/> <!--This will split the path of current page -->
<c:set var="lastValue" value="${splitURI[fn:length(splitURI)-1]}"/><!--This will give you the last value of url "360" in this case -->
<c:out value="${lastValue}"></c:out> <!--use this to make sure you are getting correct value(for testing only) -->
斯康德:
- 您必須傳遞從
http://www.domain.com/bar/{id}
獲得的/{id}
值。
使用表單作為傳遞它。
<form:form commandName="some" class="form" action="/app/same-url">
<form:input path="title"/>
<input type="hidden" name="redirect" value="bar"/>
<input type="hidden" name="path-var" value="${lastValue}"/>
<button>OK</button>
<form:form>
最后:
- 你想被重定向到
redirect:/bar/{id}"
。
這可以使用以下方法完成。
@RequestMapping(value = "/add-category", method = RequestMethod.POST)
public String handleRedirect(@RequestParam("redirect") String redirect, @RequestParam("path-var") String pathVar) {
if (redirect.equals("foo")) {
return "redirect:/foo";
} else {
return "redirect:/bar/" + pathVar;
}
}
這不是上述問題的最后/最佳解決方案。
可能有其他/更好的解決方案。
添加這個標簽庫
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
,當使用任何 jstl 函數時,就像fn:length()
。
希望這對你有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.