0

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.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
caro
  • 381
  • 3
  • 5
  • 20

1 Answers1

3

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.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank for your reply, it works :) But I have another problem after that. Now, like I said, when I'm editing worker I have URL like this: http://localhost:8080/WWP/edit/caro, but when I click back in for example button which redirect me to "/developer" I have now: http://localhost:8080/WWP/edit/developer. Instead that URL I want to have something like: http://localhost:8080/WWP/developer and in this URL my app works correctly. How to change request mapping in controller to get redirect as I want? – caro Jan 02 '13 at 18:33
  • I don't really understand the problem. Ask another question, wit the relevant code. – JB Nizet Jan 02 '13 at 18:48