在Spring中是否存在使用id进行路由的概念,就像在rails中一样

时间:2014-01-21 19:03:10

标签: spring jsp spring-mvc

如果我想在rails中获得Office's展示页面,我只需使用网址.../offices/:id

我有一个Spring应用程序,我正试图让同样的事情发生。我不确定如何编写href来转到office's showOffice页面。我有一个控制器方法:

@RequestMapping(value = "/showOffice", method=RequestMethod.GET) 
    public ModelAndView showOfficePage(Long officeId) {
        Office office = officeServiceImpl.find(officeId);
        ModelAndView result = new ModelAndView("showOffice", "command", office);
        return result;
    }

我唯一能想到的是<a href="${pageContext.request.contextPath}/showOffice(${10})" class="btn btn-primary btn-lg" role="button">

有人能指出我正确的方向吗?感谢。

1 个答案:

答案 0 :(得分:2)

是。对于这种情况,Spring 3.0及更高版本支持@PathVariable注释。有关详细信息,请参阅docs

@RequestMapping(value = "/offices/{officeId}", method=RequestMethod.GET) 
public ModelAndView showOfficePage(@PathVariable("officeId") Long officeId) {
    Office office = officeServiceImpl.find(officeId);
    ModelAndView result = new ModelAndView("showOffice", "command", office);
    return result;
}

普通旧连接可用于在视图中构建这些URL:

<a href="/offices/<%= offices.id %>" >Link</a>