1
我有一個觀點解析:添加PathVariable變化查看路徑上RequestMapping
@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
有人能解釋爲什麼我返回相同的視圖名稱在這兩種情況下,但在第二個例子中,它的行爲有所不同?
我怎樣才能使它工作?
新映射中的路徑變量是必需的,所以'/ workflowlist'不會命中'@ RequestMapping'方法,並且最終會有404.您需要設置2個映射,如下所示:http:// stackoverflow .com/a/4904139/ – approxiblue
謝謝,你突出了我的問題(我已更新)的問題。我在第二個示例中調用「http://127.0.0.1:8090/workflowlist/dev」。它在我的控制器內部發生了一個斷點,但即使我返回相同的視圖名稱,生成的視圖路徑也是不同的。爲什麼? –
@approxiblue - 你是唯一一個試圖幫助的人,所以如果你創造了一個答案,我會獎勵你的賞金,否則賞金點將消失在以太。 –