java - how to get some value from url using jstl -


i have 2 forms have same-url action, following form on page http://www.domain.com/pre-foo-url, is

<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> 

and other form on 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> 

two methods in controller, 1 deciding redirect to

@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}"; // {id} must value http://www.domain.com/bar/{id}<-- here     } }  

other method getting value of id return "redirect:/bar/{id}"; , goto /bar/{id} request mapping

@requestmapping(value = "/bar/{id}", method = requestmethod.get) public string handlebar(@pathvariable integer id) {     // logic here      return "go-any-where"; } 

now how can value http://www.domain.com/bar/{id} , set when redirect redirect:/bar/{id},

i have solution need, first must point out need write answer.

first:

-you need /{id} http://www.domain.com/bar/{id}, means want value of last part of url.

you can value adding following code on page http://www.domain.com/bar/{id}

<c:set var="currentpage" value="${requestscope['javax.servlet.forward.request_uri']}"/> <!--this give path current page eg- http://www.domain.com/bar/360 --> <c:set var="splituri" value="${fn:split(currentpage, '/')}"/> <!--this split path of current page --> <c:set var="lastvalue" value="${splituri[fn:length(splituri)-1]}"/><!--this give last value of url "360" in case --> <c:out value="${lastvalue}"></c:out> <!--use make sure getting correct value(for testing only) --> 

scond:

-you have pass value of /{id} got http://www.domain.com/bar/{id}.

pass using form as.

<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> 

at last:

-you want redirected redirect:/bar/{id}".

this done using method below.

@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;           }     }  

important

this not last/best solution problem above.

there may other/better solutions one.

add tag lib <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>, when using jstl function fn:length().

hope work you.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -