2016-08-07 75 views
0

Ich habe eine einfache Webanwendung mit SpringMVC und Hibernate erstellt. Alles funktioniert gut, sind meine Mitarbeiter-Datenbank hinzugefügt (MySQL), aber Seite zeigen nicht die Liste von ihnen:Spring MVC Hibernate Anwendung loadnig Datenbank Tabelle

that's how it look

auch nicht Logger keine Ausgabe Meldung, wenn ich hinzufüge, das Objekt, wenn es zeigen sollte so etwas wie: "Mitarbeiter erfolgreich hinzufügen, Mitarbeiterdetails: ..."

Hier ist die Seite:

<%@ 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" %> 
<%@ page session="false" %> 
<html> 
<head> 
    <title>Employee Page</title> 
    <style type="text/css"> 
     .tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;} 
     .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} 
     .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;} 
     .tg .tg-4eph{background-color:#f9f9f9} 
    </style> 
</head> 
<body> 
<h1> 
    Add a Employee 
</h1> 

<c:url var="addAction" value="/employee/add" ></c:url> 

<form:form action="${addAction}" commandName="employee"> 
    <table> 
     <c:if test="${!empty employee.name}"> 
      <tr> 
       <td> 
        <form:label path="id"> 
         <spring:message text="ID"/> 
        </form:label> 
       </td> 
       <td> 
        <form:input path="id" readonly="true" size="8" disabled="true" /> 
        <form:hidden path="id" /> 
       </td> 
      </tr> 
     </c:if> 
     <tr> 
      <td> 
       <form:label path="name"> 
        <spring:message text="Name"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="name" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2"> 
       <c:if test="${!empty employee.name}"> 
        <input type="submit" 
          value="<spring:message text="Edit Employee"/>" /> 
       </c:if> 
       <c:if test="${empty employee.name}"> 
        <input type="submit" 
          value="<spring:message text="Add Employee"/>" /> 
       </c:if> 
      </td> 
     </tr> 
    </table> 
</form:form> 
<br> 
<h3>List of Employees</h3> 
<c:if test="${!empty listEmployees}"> 
    <table class="tg"> 
     <tr> 
      <th width="80">Employee ID</th> 
      <th width="120">Employee Name</th> 
      <th width="60">Edit</th> 
      <th width="60">Delete</th> 
     </tr> 
     <c:forEach items="${listEmployee}" var="employee"> 
      <tr> 
       <td>${employee.id}</td> 
       <td>${employee.name}</td> 
       <td><a href="<c:url value='/edit/${employee.id}' />" >Edit</a></td> 
       <td><a href="<c:url value='/remove/${employee.id}' />" >Delete</a></td> 
      </tr> 
     </c:forEach> 
    </table> 
</c:if> 
</body> 
</html> 

@Qualifier(value = "employeeDAO") 
public class EmployeeDAOImpl implements EmployeeDAO { 

    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAOImpl.class); 

    private SessionFactory sessionFactory; 

    public void setSessionFactory(SessionFactory sf){ 
     this.sessionFactory = sf; 
    } 
    @Override 
    public void addEmployee(Employee e) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     session.persist(e); 
     logger.info("Employee saved successfully, Employee details: " + e); 
    } 

    @Override 
    public void updateEmployee(Employee e) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     session.update(e); 
     logger.info("Employee updated successfully, Employee details: " + e); 
    } 

    @SuppressWarnings("uncheked") 
    @Override 
    public List<Employee> listEmployee() { 
     Session session = this.sessionFactory.getCurrentSession(); 
     List<Employee> employeeList = session.createQuery("FROM Employee").list(); 
     for (Employee e: employeeList){ 
      logger.info("Employee List::" + e); 
     } 
     return employeeList; 

    } 

    @Override 
    public Employee getEmployeeById(int id) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     Employee e = (Employee) session.load(Employee.class, new Integer(id)); 
     logger.info("Employee loaded successfully, Employee details: " + e); 
     return null; 
    } 

    @Override 
    public void removeEmployee(int id) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     Employee e = (Employee) session.load(Employee.class, new Integer(id)); 
     if(e != null){ 
      session.delete(e); 
     } 
     logger.info("Employee delete successfully, Employee details: " + e); 
    } 
} 

Meine Service-Klasse:

@Service 
@Qualifier(value = "employeeService") 
public class EmployeeServiceImpl implements EmployeeService{ 



    public EmployeeServiceImpl() { 
    } 

    private EmployeeDAO employeeDAO; 

    public EmployeeServiceImpl(EmployeeDAO employeeDAO) { 
     this.employeeDAO = employeeDAO; 
    } 

    @Override 
    @Transactional 
    public void addEmployee(Employee e) { 
     this.employeeDAO.addEmployee(e); 
    } 

    @Override 
    @Transactional 
    public void updateEmployee(Employee e) { 
     this.employeeDAO.updateEmployee(e); 
    } 

    @Override 
    @Transactional 
    public List<Employee> listEmployee() { 
     return this.employeeDAO.listEmployee(); 
    } 

    @Override 
    @Transactional 
    public Employee getEmployeeById(int id) { 
     return this.employeeDAO.getEmployeeById(id); 
    } 

    @Override 
    @Transactional 
    public void removeEmployee(int id) { 
     this.employeeDAO.removeEmployee(id); 
    } 

    public void setEmployeeDAO(EmployeeDAOImpl employeeDAO) { 
     this.employeeDAO = employeeDAO; 
    } 
} 

Hier erstelle ich die Abbildung:

@Entity 
@Table(name = "Employee") 
public class Employee { 
    @Column(name = "Name") 
    private String name; 

    @Id 
    @Column(name = "Employee ID") 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int employeeID; 

    public Employee(String name) { 
     this.name = name; 
    } 

    public Employee() { 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getEmployeeID() { 
     return employeeID; 
    } 

    public void setEmployeeID(int employeeID) { 
     this.employeeID = employeeID; 
    } 

    @Override 
    public String toString() { 
     return "Employee{" + 
       "name='" + name + '\'' + 
       ", employeeID=" + employeeID + 
       '}'; 
    } 
} 

Controller-Klasse:

@Controller 
public class EmployeeController { 

    private EmployeeService employeeService; 

    @Autowired(required = true) 
    @Qualifier("employeeService") 
    public void setEmployeeService(EmployeeService employeeService) { 
     this.employeeService = employeeService; 
    } 

    @RequestMapping(value = "/employee", method = RequestMethod.GET) 
    public String employeeList(Model model) { 
     model.addAttribute("employee", new Employee()); 
     model.addAttribute("listEmployee", this.employeeService.listEmployee()); 
     return "employee"; 
    } 

    //For add and update person both 
    @RequestMapping(value = "/employee/add", method = RequestMethod.GET) 
    public String addEmployee(@ModelAttribute("person") Employee e){ 

     if (e.getEmployeeID() == 0){ 
      //new employee, add it 
      this.employeeService.addEmployee(e); 
     }else { 
      //existing employee, call update 
      this.employeeService.updateEmployee(e); 
     } 
     return "redirect:/employee"; 
    } 

    @RequestMapping(value = "employees/remove/{id}") 
    public String removeEmployee(@PathVariable("id") int id){ 

     this.employeeService.removeEmployee(id); 
     return "redirect:/employees"; 
    } 

    @RequestMapping(value = "/employees/edit{id}") 
    public String editEmployee(@PathVariable("id") int id, Model model){ 

     model.addAttribute("employee", this.employeeService.getEmployeeById(id)); 
     model.addAttribute("listEmployees", this.employeeService.listEmployee()); 
     return "employee"; 
    } 
} 

Antwort

0

In JSP Seite Ihre Prüfzustand listEmployees, aber in-Controller Sie haben als Modell hinzugefügt Attribut listEmployee. Entferne die letzten Zeichen aus listEmployees in Jsp. Es sollte funktionieren. Ansonsten ist alles in Ordnung.

<c:if test="${!empty listEmployees}"> 

Kontroller

model.addAttribute("listEmployee", this.employeeService.listEmployee()); 
0

Änderung

<c:forEach items="${listEmployee}" var="employee"> 

zu

<c:forEach items="${listEmployees}" var="employee">