Spring MVC Request URLs in JSP

Viewed 74923

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 application (form actions, <a> tags, etc.) Current, I have this (obviously abbreviated):

//In the controller
@RequestMapping(value="/admin/listPeople", method=RequestMethod.GET)

//In the JSP
<a href="/admin/listPeople">Go to People List</a>

When I directly enter the URL like "http://localhost:8080/MyApp/admin/listPeople", the page loads correctly. However, the link above does not work. It looses the application name "MyApp".

Does anyone know if there is a way to configure Spring to throw on the application name on there?

Let me know if you need to see any of my Spring configuration. I am using the standard dispatcher servlet with a view resolver, etc.

7 Answers

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

<spring:url value="/new" var="url" htmlEscape="true"/>
<a href="${url}">New consultant</a>

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

Related