检索JSP中的部分URL路径

时间:2014-06-15 21:50:04

标签: java spring jsp

在我的spring应用程序中,我在我的控制器中有一个方法以这种形式映射视图:

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

我希望在jsp中检索viewname(1,10和1)之后的值。

有没有办法做到这一点?

那是我的JSP页面:

<%@ 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" %>

我需要在上面的代码<table>之前检索此数据。

2 个答案:

答案 0 :(得分:1)

只需从请求中获取URL,在URL中找到viewname的索引然后中断字符串,最后根据/将其拆分以获取数字数组。

查看此示例代码中广泛使用的JSP JSTL Functions

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

示例代码:(替换上面的前两行)

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

答案 1 :(得分:0)

尝试了几种方法后,我的最终代码使用注释PathVariable将值广播到视图和隐藏字段以存储它们。像这样:

控制器中的

@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;
}
jsp页面中的

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