Spring ModelAndView.setViewName(" view")在错误的位置搜索

时间:2015-03-09 23:00:17

标签: java spring jsp spring-mvc spring-boot

我现在面临一个奇怪的问题。在我的GET方法中,我指定了viewName。但是Spring会去另一个地方搜索它。

    @Controller
@RequestMapping(value = "/Admin")
public class AdminTaskController {

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    CSVValidation csvValidation;

    @RequestMapping(value = "/Task/ViewAll", method = RequestMethod.GET)
    public ModelAndView getAllTasks() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("viewAllTasks");
        List<Task> tasks = (List<Task>) taskDao.findAll();
        modelAndView.addObject("tasks", tasks);
        return modelAndView;
    }

}

但根据调试:

    o.s.w.s.v.ContentNegotiatingViewResolver:327 - Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
    o.s.w.s.v.ContentNegotiatingViewResolver:402 - Returning [org.springframework.web.servlet.view.JstlView: name 'Admin/Task/ViewAll'; URL [/WEB-INF/jsp/Admin/Task/ViewAll.jsp]] based on requested media type 'text/html'
    o.s.w.s.DispatcherServlet:1218 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'Admin/Task/ViewAll'; URL [/WEB-INF/jsp/Admin/Task/ViewAll.jsp]] in DispatcherServlet with name 'dispatcherServlet'
    o.s.w.s.v.JstlView:377 - Added model object 'modelAndView' of type [org.springframework.web.portlet.ModelAndView] to request in view with name 'Admin/Task/ViewAll'
    o.s.w.s.v.JstlView:377 - Added model object 'org.springframework.validation.BindingResult.modelAndView' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'Admin/Task/ViewAll'
o.s.w.s.v.JstlView:207 - Forwarding to resource [/WEB-INF/jsp/Admin/Task/ViewAll.jsp] in InternalResourceView 'Admin/Task/ViewAll'

ContentNegotiatingViewResolver去搜索/WEB-INF/jsp/Admin/Task/文件夹以查找ViewAll.jsp。它不包含这样的内容。

viewAllTasks.jsp包含在/WEB-INF/jsp/文件夹中。因为如果它没有创建正确的视图并给出404:

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

1 个答案:

答案 0 :(得分:1)

名称空间(导入)存在问题。我正在使用

import org.springframework.web.portlet.ModelAndView;

而不是

import org.springframework.web.servlet.ModelAndView;

那真是太不幸了:(