I have a Spring (v, 4.3.2 & Java 8) application that serves up an Angular2 single page application front end that has multiple routes (say /foo
, /bar
and /baz
, one of which requires a parameter to render ( /baz/x
where x is the identifier of the resource to retrieve).
The html is served up with a standard Spring MVC @Controller
:
@RequestMapping(value = {"/", "/foo", "/bar", "/baz/{id}"}, method = RequestMethod.GET)
public String index(@PathVariable Optional<String> id) {
return "index";
}
and views are resolved using:
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
If the browser reloads from the /
, /foo
or /bar
routes, the controller properly returns the index
page and angular resolves the view for the route as expected. However, if the browser reloads from one of the baz
routes that contain a paramenter, the server prepends baz
to the resource path (eg /baz/WEB-INF/pages/index.jsp
) which, results in a 404 error.
I've tried refactoring the baz
request mappings info into an annotation on a distinct (identical) method and using wildcards in the request mapping (eg /**
and baz/*
), but always get the same result. Regardless of the request mapping, Spring seems unaware that it is appending baz
into the path, as the logs contain:
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'index'; URL [WEB-INF/pages/index.jsp]] in DispatcherServlet with name 'dispatcher'
DEBUG: org.springframework.web.servlet.view.InternalResourceView - Forwarding to resource [WEB-INF/pages/index.jsp] in InternalResourceView 'index'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
Any help on this would be appreciated.
Solved the problem - omitting the leading slash from the prefix argument string causes the issue. For proper configuration the argument should be:
viewResolver.setPrefix("/WEB-INF/pages/");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.