简体   繁体   中英

Retrieve part of URL path in JSP

In my spring application, I have in my controller one method map a view in this form:

http://server/app/viewname/1/10/1

I want retrieve inside the jsp the values after the viewname (1, 10 and 1).

Is there any way to do that?

That's my JSP page:

<%@ include file="../include/header.jsp" %>

<sec:authorize access="hasPermission(#user, 'cadastra_${param.name}')">
<p>
    <button type="button" class="btn btn-sm btn-link link" data-action="/${param.name}/cadastra">
        Cadastrar novo ${param.name}
    </button>
</p>
</sec:authorize>

<table class="bordered">
    <thead>
        <tr>
        <c:forEach var="item" items="${param.elements}">
            <th class="col"> ${item} </th>
        </c:forEach>
        </tr>
    </thead>

    <tbody class="content">
    </tbody>

    <tfoot>
        <tr>
            <sec:authorize access="hasPermission(#user, 'altera_${param.name}')">
                <td class="comando" data-nome="Altera" data-action="/${param.name}/altera"></td>
            </sec:authorize>
            <sec:authorize access="hasPermission(#user, 'remove_${param.name}')">
                <td class="comando" data-nome="Remove" data-action="/${param.name}/remove"></td>
            </sec:authorize>
        </tr>
    </tfoot>
</table>

<c:url value="/${param.name}/listagem.json" var="listagem"/>
<script>
$(document).ready(function(){
    load_content("${listagem}", $('table.bordered'));
});
</script>

<%@ include file="../include/footer.jsp" %>

I need retrieve this data before the tag <table> above.

Simply get the URL from the request, find the index of viewname in the URL then break the string and finally split it based on / to get the array of digits.

Have a look at JSP JSTL Functions that is extensively used in this sample code.

<c:set var="req" value="${pageContext.request}" />
<c:set var="url">${req.requestURL}</c:set>
<c:set var="viewname" value="${param.name}" />

Sample code: (replace first 2 lines form above)

<c:set var="url" value="http://localhost:8080/server/app/viewname/1/10/1" />
<c:set var="viewname" value="viewname" />
<c:set var="numbers"
    value="${fn:substring(url, fn:indexOf(url,viewname)+fn:length(viewname)+1,fn:length(url)) }" />

<c:forEach var="number" items="${fn:split(numbers,'/') }">
    <c:out value="${number }" />
</c:forEach>

After try several ways, my final code uses the annotation PathVariable to broadcast the values to the view and hidden fields to store them. Like this:

in the controller

@RequestMapping(value="listagem/{pagina}/{items}/{ordem}")
@PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)")
public ModelAndView listagem(@PathVariable("pagina") String pagina, @PathVariable("items") String items, @PathVariable("ordem") String ordem) {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("privado/"+this.getName()+"/listagem");

    mav.addObject("lista", serv.listagem());
    mav.addObject("pagina", pagina);
    mav.addObject("items", items);
    mav.addObject("ordem", ordem);

    return mav;
}

@RequestMapping(value="listagem.json", method=RequestMethod.GET)
@PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)")
public ModelAndView listagem_json(@RequestParam("pagina") String pagina, @RequestParam("items") String items, @RequestParam("ordem") String ordem) {
    ModelAndView mav = new ModelAndView();
    mav.setViewName(this.getName()+"/listagem");

    mav.addObject("lista", serv.listagem());
    mav.addObject("pagina", pagina);
    mav.addObject("items", items);
    mav.addObject("ordem", ordem);

    return mav;
}

in the jsp page

<input type="hidden" name="pagina" value="${pagina}">
<input type="hidden" name="items" value="${items}">
<input type="hidden" name="ordem" value="${ordem}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM