我有一个视图解析器:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
和控制器:
@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);
}
}
当我调用页面http://127.0.0.1:8090/workflowlist
时,它会从src/main/webapp/WEB-INF/jsp/workflowlist.jsp
提供jsp。这一切似乎都运作良好。
但是,当我尝试添加@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);
}
当我致电页面http://127.0.0.1:8090/workflowlist/dev
时,会发出以下消息:
There was an unexpected error (type=Not Found, status=404).
/workflowlist/WEB-INF/jsp/workflowlist.jsp
有人可以解释为什么我在两种情况下都返回相同的视图名称,但在第二个例子中它的行为有所不同吗?
我怎样才能让它发挥作用?
答案 0 :(得分:1)
问题在于我的viewResolver
:
resolver.setPrefix("WEB-INF/jsp/");
应该是:
resolver.setPrefix("/WEB-INF/jsp/");
前面有一个/
,路径是从根目录(webapps文件夹)中获取的,但是当/
丢失时,它将成为相对路径。
我从来没有得到一个答案,为什么视图解析器只占用了路径的目录部分,但这似乎发生了什么。 可能因此您可以定义具有不同根的视图子树。