1

I have a view resolver:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}

and a controller:

@Controller
public class WorkflowListController {

 @RequestMapping(path = "/workflowlist", method = RequestMethod.GET)
 public ModelAndView index() throws LoginFailureException, PacketException,
        NetworkException {

    String profile = "dev";
    List<WorkflowInformation> workflows = workflows(profile);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("profile", profile);
    map.put("workflows", workflows);
    return new ModelAndView("workflowlist", map);
 }
}

and when I call the page http://127.0.0.1:8090/workflowlist it serves the jsp from src/main/webapp/WEB-INF/jsp/workflowlist.jsp. That all seems to work well.

However when I try to add a @PathVariable:

@RequestMapping(path = "/workflowlist/{profile}", method = RequestMethod.GET)
public ModelAndView workflowlist(@PathVariable String profile)
        throws LoginFailureException, PacketException, NetworkException {

    List<WorkflowInformation> workflows = workflows(profile);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("profile", profile);
    map.put("workflows", workflows);
    return new ModelAndView("workflowlist", map);
}

When I call the page http://127.0.0.1:8090/workflowlist/dev gives the following message:

There was an unexpected error (type=Not Found, status=404).
/workflowlist/WEB-INF/jsp/workflowlist.jsp

Can someone explain why I'm returning the same view name in both cases but in the second example it is behaving differently?

How can I get it to work?

David Newcomb
  • 10,639
  • 3
  • 49
  • 62
  • The path variable in the new mapping is required, so `/workflowlist` does not hit the `@RequestMapping` method and you end up with 404. You need to set up 2 mappings, like this: http://stackoverflow.com/a/4904139/ – approxiblue Mar 08 '16 at 01:38
  • Thanks, you highlighted a problem with my question (which I've updated). I am calling `http://127.0.0.1:8090/workflowlist/dev` in the second example. It is hitting a break point inside my controller but the generated view path is different even though I'm returning the same view name. Why? – David Newcomb Mar 08 '16 at 13:30
  • @approxiblue - you were the only person who tried to help, so if you create an answer, I'll award you the bounty otherwise the bounty points will disappear into the ether. – David Newcomb Mar 14 '16 at 23:50
  • Anything else I should elaborate on? I'd rather not repeat your answer. – approxiblue Mar 15 '16 at 01:29
  • If you know why the view resolver only takes the directory part and explain my last paragraph, then that would be nice. – David Newcomb Mar 15 '16 at 12:30
  • Yeah, not sure why the view resolver works like that. I guess it just works. :) – approxiblue Mar 15 '16 at 23:53

1 Answers1

1

The problem was with my viewResolver:

resolver.setPrefix("WEB-INF/jsp/");

should have been:

resolver.setPrefix("/WEB-INF/jsp/");

With a / at the front the path is taken from the root (webapps folder) but when the / is missing it becomes a relative path.

I never got an answer as to why the view resolver only took the directory part of the path but that's what appeared to happen. It's probably so you can define subtrees of views with different roots.

David Newcomb
  • 10,639
  • 3
  • 49
  • 62