[英]Spring MVC returns a 404 because of a wrong InternalResourceViewResolver view name
I am new on Spring MVC & Boot. 我是Spring MVC和Boot的新手。 I can open http://localhost:8088/postList but I am experiencing Whitelabel Error Page error when opening http://localhost:8088/post/1 .
我可以打开http:// localhost:8088 / postList,但是打开http:// localhost:8088 / post / 1时遇到Whitelabel Error Page错误。 I can't find my mistake.
我找不到我的错误。 Can you say it?
你能说吗
My project structure 我的项目结构
My InternalResourceViewer: 我的InternalResourceViewer:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
My Controller: 我的控制器:
@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;
}
}
My PostList: 我的帖子列表:
My Post viewing page: 我的帖子查看页面:
My postList.jsp taglibs and contents: 我的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>
The short answer is that you need a leading /
in your InternalResourceViewResolver
prefix. 简短的答案是,您在
InternalResourceViewResolver
前缀中需要一个前导/
。 So 所以
resolver.setPrefix("/WEB-INF/views/");
The long answer is that Spring MVC uses the InternalResourceViewResolver
to generate a View
, in this case a JstlView
. 长的答案是Spring MVC使用
InternalResourceViewResolver
生成一个View
,在这种情况下为JstlView
。 Spring MVC then tries to render this View
. 然后,Spring MVC尝试渲染此
View
。
To do so, it uses the view name (the prefix, the name in your ModelView
, and the suffix, ie. WEB-INF/views/post.jsp
), as a path and tries to retrieve a RequestDispatcher
by delegating to ServletRequest#getRequestDispatcher(String)
. 要做到这一点,它使用的视图名称(前缀,在你的名字
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.
指定的路径名可以是相对的 ,尽管它不能扩展到当前servlet上下文之外。 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
.如果Servlet容器无法返回
RequestDispatcher
则此方法返回null
。
Since you don't have a leading /
, it is relative to the current path, ie. 由于您没有前导
/
,所以它相对于当前路径,即。 /post/1
. /post/1
。 That makes it /post/WEB-INF/views/post.jsp
. 这使其成为
/post/WEB-INF/views/post.jsp
。 And since you have no such resource relative to the ServletContext
, your Servlet container returns a 404. 并且由于没有相对于
ServletContext
资源,因此Servlet容器返回404。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.