2016-06-29 19 views
0

我是Spring mvc的新手。目前我正在嘗試創建一個網頁來使用spring mvc執行視圖和創建Employee Object。但是,在我的網頁查看員工的不顯示,數據不會顯示在每個循環的jsp頁面中使用

我的控制器是遵循

package com.testapp.springmvc.controllers; 

import java.util.HashMap; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.testapp.springmvc..models.Employee; 

@Controller 
public class TestController { 
    HashMap<Integer, Employee> employees = new HashMap<>(); 
    Integer count = 0; 

    @RequestMapping(value = "/employee", method = RequestMethod.GET) 
    public String employee(Model model) { 
     model.addAttribute("employee", new Employee()); 
     model.addAttribute("listOfEmployee", employees.values()); 
     return "employee"; 
    } 

    @RequestMapping(value = "/employee", method = RequestMethod.POST) 
    public String addEmployee(@ModelAttribute("employee") Employee employee) { 
     employees.put(++count, employee); 
     return "redirect:employee"; 
    } 
} 

我的JSP的觀點是按照事先

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <form:form action="employee" commandName="employee" method="post"> 
     <table> 
      <tr> 
       <td><form:input path="id" disable="true" /></td> 
      </tr> 
      <tr> 
       <td>Name of Employee <br> <form:input type="text" 
         path="fname" name="fname" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Surname of Employee <br> <form:input path="lname" 
         type="text" name="lname" /> 
      </tr> 
      <tr> 
       <td><input type="submit" value="Add Employee" /></td> 
      </tr> 
     </table> 
    </form:form> 
    <c:if test="${!empty listOfEmployee}"> 
     <table> 
      <tr> 
       <th>Employee Name</th> 
       <th>Surname</th> 
      </tr> 
      <c:forEach items="${listOfEmployee}" var="employee"> 
       <tr> 
        <td>${employee.fname}</td> 
        <td>${employee.lname}</td> 
       </tr> 
      </c:forEach> 
     </table> 
    </c:if> 

</body> 
</html> 

感謝。

+0

你從控制器調用哪種方法? –

+0

當jsp加載employee()方法被調用時,當我們點擊提交按鈕時,addEmployee()被調用。 – nitika

+0

爲什麼你要放這個model.addAttribute(「employee」,new Employee());因爲你聲明僱員對象的哈希映射 – PacMan

回答

0

上述代碼不起作用,因爲在我的web.xml文件中,我正在使用2.3 dtd,將dtd定義升級到3.1代碼後工作正常。

0

所以哈希映射將永遠不會滿足僱員對象這是正常的,因爲你沒有實現業務邏輯!嘗試在控制器中創建Employees對象(即使不推薦),然後將它們添加到hashMap中,然後在默認情況下使用GET方法加載頁面「/ employee」時,它會列出您已填寫完畢的職位你的hashMap。

+0

我正在使用addEmployee()方法將員工對象添加到散列表中。您的表單中的 – nitika

+0

嘗試添加爲屬性modelAttribute =「employee」 – PacMan

相關問題