2016-04-02 51 views
0

我有映射和ModelAndView的問題。所有簡單的映射,比如「/登錄」的作品,但我有一個問題:Spring RequestMapping錯誤視圖路徑

@RestController 
@RequestMapping("/note/{noteId:[\\d]+}") 
public class NoteController { 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView note(@PathVariable String noteId, HttpSession session) { 
    // not important code 
    ModelAndView modelAndView = new ModelAndView("note"); 
    return modelAndView; 
    } 
} 

我這裏有一個錯誤和路徑:/note/WEB-INF/views/jsp/note.jsp

我想得到WEB-INF/views/jsp/note.jsp,我在其他ModelAndViews(沒有前綴)中有類似的路徑。

我的配置:

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

用@ @ Controller替換'@ RestController'。 –

+0

有什麼區別? –

+3

'@ RestController'返回一切作爲響應正文,它會自動將您的'ModelAndView'作爲JSON或XML返回。 '@ Controller'確實有合適的視圖分辨率。你的前綴也應該是'/ WEB-INF/views/jsp /'。包含前導'/'。 –

回答

2

M. Deinum解決了這個問題。

與@Controller

@RestController返回的一切作爲響應體,它會自動返回你的ModelAndView作爲JSON或XML替換@RestController。 @Controller做適當的視圖解析。你的前綴也應該是/ WEB-INF/views/jsp /。包括領導/。