2016-12-22 9 views
1

私はSpring MVC &で新しくなっています。 http://localhost:8088/postListを開くことができますが、http://localhost:8088/post/1を開くとWhitelabel Error Pageエラーが発生します。私は間違いを見つけることができません。あなたはそれを言うことができますか?Spring MVCはInternalResourceViewResolverビュー名が間違っているため404を返します

マイプロジェクト構造

enter image description here

マイ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; 
} 


} 

マイPostList:

enter image description here

私のポストの閲覧ページ:

enter image description here

マイ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> 
+0

リンクを含む 'postList.jsp'コンテンツを表示します。 –

答えて

1

短い答えは、あなたが必要だということです先導/にはInternalResourceViewResolverというプレフィックスが付きます。だから、

resolver.setPrefix("/WEB-INF/views/"); 

長い答えは、Spring MVCのは、この場合JstlViewで、Viewを生成するInternalResourceViewResolverを使用していることです。その後、Spring MVCはこれをレンダリングしようとしますView。そうするには

は、それがパスとして、ビュー名(接頭辞、あなたのModelViewでの名前、および接尾辞、すなわち。WEB-INF/views/post.jsp)を使用してServletRequest#getRequestDispatcher(String)に委譲することによってRequestDispatcherを取得しようとします。

それが現在のサーブレット・コンテキスト外 を拡張することはできないが指定されたパス名は、相対であってもよいです。 パスが"/"で始まる場合、 は現在のコンテキストルートとの相対的なものとして解釈されます。このメソッド は、サーブレットコンテナが RequestDispatcherを返すことができない場合はnullを返します。

先頭には/が含まれていないため、現在のパスに相対的です。 /post/1。それは/post/WEB-INF/views/post.jspになります。 ServletContextに関連するリソースがないため、サーブレットコンテナは404を返します。

+0

どのように私はそれを逃した。そんなにSotiriosありがとうございました。接頭辞の "/"の頭に追加すると解決します。 – user2400092

関連する問題