Spring MVC returns 404,因为 InternalResourceViewResolver 视图名称错误
Spring MVC returns a 404 because of a wrong InternalResourceViewResolver view name
我是 Spring MVC 和引导的新手。我可以打开 http://localhost:8088/postList but I am experiencing Whitelabel Error Page error when opening http://localhost:8088/post/1 。我找不到我的错误。可以说吗?
我的项目结构
我的 InternalResourceViewer:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
我的控制器:
@Controller
@RequestMapping("/")
public class PostController
{
@Autowired
PostService postService;
@RequestMapping(value="/post/{id}", method=RequestMethod.GET)
public ModelAndView list(@PathVariable("id") int id){
ModelAndView mav=new ModelAndView("post");
Post postItem=postService.getPostById(id);
mav.addObject("postItem",postItem);
mav.addObject("postItem",postItem);
return mav;
}
@RequestMapping(value="/postList", method=RequestMethod.GET)
public ModelAndView postlist(){
ModelAndView mav=new ModelAndView("postList");
mav.addObject("postList",postService.listPosts());
return mav;
}
}
我的Post列表:
我的 Post 查看页面:
我的 postList.jsp 标签库和内容:
<div class="row">
<c:if test="${not empty postList}">
<c:forEach var="postItem" items="${postList}">
<div class="col-lg-8">
<h1><a href="<c:url value='/post/${postItem.id}' />">${postItem.header}</a></h1>
<p class="lead">
by <a href="#">${postItem.user_id}</a>
</p>
<p><span class="glyphicon glyphicon-time"></span>${postItem.upddate}</p>
<hr>
</div>
</c:forEach>
</c:if>
简短的回答是您的 InternalResourceViewResolver
前缀中需要一个前导 /
。所以
resolver.setPrefix("/WEB-INF/views/");
长答案是 Spring MVC 使用 InternalResourceViewResolver
生成 View
, in this case a JstlView
。 Spring MVC 然后尝试呈现此 View
。
为此,它使用视图名称(前缀,ModelView
中的名称和后缀,即 WEB-INF/views/post.jsp
)作为路径并尝试检索 RequestDispatcher
通过委派给 ServletRequest#getRequestDispatcher(String)
.
The pathname specified may be relative, although it cannot extend
outside the current servlet context. If the path begins with a "/"
it
is interpreted as relative to the current context root. This method
returns null
if the servlet container cannot return a
RequestDispatcher
.
由于您没有前导 /
,它是相对于当前路径的,即。 /post/1
。这使得它 /post/WEB-INF/views/post.jsp
。并且由于您没有相对于 ServletContext
的此类资源,因此您的 Servlet 容器 returns a 404。
我是 Spring MVC 和引导的新手。我可以打开 http://localhost:8088/postList but I am experiencing Whitelabel Error Page error when opening http://localhost:8088/post/1 。我找不到我的错误。可以说吗?
我的项目结构
我的 InternalResourceViewer:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
我的控制器:
@Controller
@RequestMapping("/")
public class PostController
{
@Autowired
PostService postService;
@RequestMapping(value="/post/{id}", method=RequestMethod.GET)
public ModelAndView list(@PathVariable("id") int id){
ModelAndView mav=new ModelAndView("post");
Post postItem=postService.getPostById(id);
mav.addObject("postItem",postItem);
mav.addObject("postItem",postItem);
return mav;
}
@RequestMapping(value="/postList", method=RequestMethod.GET)
public ModelAndView postlist(){
ModelAndView mav=new ModelAndView("postList");
mav.addObject("postList",postService.listPosts());
return mav;
}
}
我的Post列表:
我的 Post 查看页面:
我的 postList.jsp 标签库和内容:
<div class="row"> <c:if test="${not empty postList}"> <c:forEach var="postItem" items="${postList}"> <div class="col-lg-8"> <h1><a href="<c:url value='/post/${postItem.id}' />">${postItem.header}</a></h1> <p class="lead"> by <a href="#">${postItem.user_id}</a> </p> <p><span class="glyphicon glyphicon-time"></span>${postItem.upddate}</p> <hr> </div> </c:forEach> </c:if>
简短的回答是您的 InternalResourceViewResolver
前缀中需要一个前导 /
。所以
resolver.setPrefix("/WEB-INF/views/");
长答案是 Spring MVC 使用 InternalResourceViewResolver
生成 View
, in this case a JstlView
。 Spring MVC 然后尝试呈现此 View
。
为此,它使用视图名称(前缀,ModelView
中的名称和后缀,即 WEB-INF/views/post.jsp
)作为路径并尝试检索 RequestDispatcher
通过委派给 ServletRequest#getRequestDispatcher(String)
.
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a
"/"
it is interpreted as relative to the current context root. This method returnsnull
if the servlet container cannot return aRequestDispatcher
.
由于您没有前导 /
,它是相对于当前路径的,即。 /post/1
。这使得它 /post/WEB-INF/views/post.jsp
。并且由于您没有相对于 ServletContext
的此类资源,因此您的 Servlet 容器 returns a 404。