2017-02-05 14 views
2

私のJSPのビューに値リストを表示したいのですが、これはできません。Springを使用してJSPの値のリストを表示する

下記のコントローラクラスは、ModelAndViewマップにリストを追加するだけで、index.jspページにリダイレクトされます。

EmployeeController

@Controller 
public class EmployeeController { 

@RequestMapping(value={"/employee"}, method = RequestMethod.GET) 
public String listEmployee(){  
    System.out.println("Kontroler EmployeeController"); 
    LinkedList<String> list = getList(); 
    ModelAndView map = new ModelAndView("index"); 
    map.addObject("lists", list); 

    return map.getViewName(); 
} 

private LinkedList<String> getList(){ 
    LinkedList<String> list = new LinkedList<>(); 

    list.add("Item 1"); 
    list.add("Item 2"); 
    list.add("Item 3"); 

    return list; 
} 

} 

index.jspを

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Welcome to Spring Web MVC project</title> 
</head> 

<body> 
    <h1>Index page</h1> 
    <h1>${msg}</h1> 
    <a href="/MavenHello/employee">Zaměstnanci</a> 
</body> 
    <c:if test="${not empty listEmployee}"> 

    <ul> 
     <c:forEach var="listValue" items="${listEmployee}"> 
      <li>${listValue}</li> 
     </c:forEach> 
    </ul> 

</c:if> 

たびに、私は"Zaměstnanci"を打つので、私は、コントローラにアクセスすることができる午前、System.out.println("Kontroler EmployeeController")プリント"Kontroler EmployeeController"でTomcatにログ。ただし、index.jspページは空白です。

助けてもらえますか?

答えて

0

あなたはドキュメントに記載されているように、データなしで名前の名前だけを返したmap.getViewName()ModelAndViewリターンのModelAndView自体を移入していないされたよう:

public String getViewName() によって解決されるビュー名を返します。DispatcherServlet ViewResolver経由で、またはView オブジェクトを使用している場合はnullを返します。

次のように:第二に、あなたはリストに与えられたあなたのインデックスページや変数名にJSTLタグ<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>が欠落している

@RequestMapping(value = { "/employee" }, method = RequestMethod.GET) 
public ModelAndView listEmployee() { 
    System.out.println("Kontroler EmployeeController"); 
    LinkedList<String> list = getList(); 
    ModelAndView map = new ModelAndView("index"); 
    map.addObject("lists", list); 

    return map; 
} 

は、「リスト」であるので、むしろ「listEmployee」よりも「リスト」を反復処理、次のように:

<html> 
<head> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Welcome to Spring Web MVC project</title> 
</head> 

<body> 
    <h1>Index page</h1> 
</body> 

<c:if test="${not empty lists}"> 
    <c:forEach items="${lists}" var="lists"> 
     ${lists} 
</c:forEach> 
</c:if> 

また、あなたのクラスパスにJSTL依存性を持っていることを確認してください。

<dependency> 
    <groupId>jstl</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.2</version> 
</dependency> 
+1

あなたの投稿に表示されているようにすべてのファイルを更新しましたが、すべて正常に動作しています。ありがとうございます:) –

1

コントローラメソッドparamsにModelを追加し、このモデルに属性を追加するだけです。これを行うには

RequestMapping(value={"/employee"}, method = RequestMethod.GET) 
public String listEmployee(Model model){  
    System.out.println("Kontroler EmployeeController"); 
    LinkedList<String> list = getList(); 
    model.addAttribute("lists", list); 

    return "index"; 
} 

もう一つの方法は、ちょうどあなたのために優れている方法を選択代わりにString

@RequestMapping(value={"/employee"}, method = RequestMethod.GET) 
public ModelAndView listEmployee(){  
    System.out.println("Kontroler EmployeeController"); 
    LinkedList<String> list = getList(); 
    ModelAndView map = new ModelAndView("index"); 
    map.addObject("lists", list); 

    return map; 
} 

ModelAndViewを返すことです。

そしてまた、あなたがlistsないlistEmployee`として属性を持つModelAndViewのようにlistsにあなたのインデックスページでlistEmployeeを変更。

+0

最初のケースでは、 "index" - > StringをModelAndViewに変換できず、2番目のケースでurl/MavenHello/WEB-INF/jsp/employeeにエラー404が表示されます。 jsp、今私は以前よりも混乱しています:D –

+0

@StepanMocanu申し訳ありませんが、私はタイプミスがありました。最初のケースでは、メソッドがStringを返すようにする必要があります。私はそれをすでに変更しました –

0

変更キーの名前と、それを試してみてください。listEmployee --> lists as you are setting it as map.addObject("lists", list);

<ul> 
    <c:forEach var="listValue" items="${lists}"> 
     <li>${listValue}</li> 
    </c:forEach> 
</ul> 
関連する問題