I am new on 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 . I can't find my mistake. Can you say it?
My project structure
My 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:
<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. 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 then tries to render this 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)
.
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
.
Since you don't have a leading /
, it is relative to the current path, ie. /post/1
. That makes it /post/WEB-INF/views/post.jsp
. And since you have no such resource relative to the ServletContext
, your Servlet container returns a 404.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.