Question

I have a little problem with my app in Spring MVC. I want to edit users data in my app. So I have edit controller which has listWorkers, redirectWorker and editWorker method.

@RequestMapping("/print")
public String listWorkers(Model model)
{
    model.addAttribute("workerList", workerService.getAllWorkers());
    return "print";
}

@RequestMapping("/edit")
public String redirectWorker(HttpServletRequest request)
{
    String parameter = request.getParameter("workers");
    String path = "redirect:/edit/" + parameter;
    return path;
}

    @RequestMapping("/edit/{worker}")
public String editWorker(@PathVariable("worker")
String login, Model model)
{
    model.addAttribute("worker", workerService.getWorker(login));
    return "edition";
}

I have a problem with resources and images folders. When I'm using for example, print method everything is good but when I try to use editWorker method my logo and css files are not loading. I have a request mapping for this folders in servlet-context file:

<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/images/**" location="/images/" />

When I'm using print method I have URL like this:

http://localhost:8080/WWP/print

and I can see image and style. But when I'm using edit method on my specific user I have URL like this:

http://localhost:8080/WWP/edit/caro

And in this way I can't see image and style. I have a warn:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WWP/edit/resources/images/logo.png] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WWP/edit/resources/styles/menu.css] in DispatcherServlet with name 'appServlet'

I have images and styles catalogues in resource folder which is directly in webapp folder (cause I'm using maven). It works earlier, but when I have something like /*/* it doesn't work. I suppose that something is wrong with resurces mapping in my servlet-context file.

I' m loading my css and image file like this:

<img src="resources/images/logo.png">
<link rel="stylesheet" href="resources/styles/menu.css" type="text/css"></link>

Thanks in advance for your help.

Was it helpful?

Solution

Don't use relative paths. Use absolute paths instead:

<img src="<c:url value='/resources/images/logo.png'/>">
<link rel="stylesheet" href="<c:url value='/resources/styles/menu.css'/>" type="text/css"></link>

The JSTL <c:url> tag makes sure the context path is prepended to the absolute path passed as argument in the value attribute, so if your webapp is deployed to http://localhost:8080/WWP, the generated HTML code will be

<img src="/WWP/resources/images/logo.png">
<link rel="stylesheet" href="/WWP/resources/styles/menu.css" type="text/css"></link>

The <c:url> tag should also be used for every other URL: anchor hrefs, form actions, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top