Spring MVC Request URLs in JSP

后端 未结 7 1995
名媛妹妹
名媛妹妹 2020-12-13 15:30

I am writing a web application using Spring MVC. I am using annotations for the controllers, etc. Everything is working fine, except when it comes to actual links in the app

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 15:45

    Since it's been some years I thought I'd chip in for others looking for this. If you are using annotations and have a controller action like this for instance:

    @RequestMapping("/new")   //<--- relative url
    public ModelAndView newConsultant() {
        ModelAndView mv = new ModelAndView("new_consultant");
        try {
            List list = ConsultantDAO.getConsultants();
            mv.addObject("consultants", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
    

    in your .jsp (view) you add this directive

    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    

    and simply use

    
    New consultant
    

    where

    value's value should match @RequestMapping's argument in the controller action and

    var's value is the name of the variable you use for href

    HIH

提交回复
热议问题