使用spring hibernate无法在浏览器上显示数据。下面是我的代码和输出。
类:CustomerController
package com.luv2code.springdemo.controller;
import com.luv2code.springdemo.DAO.CustomerDAO;
import com.luv2code.springdemo.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerDAO customerDAO;
@RequestMapping("/list")
public String listCustomer(Model theModel){
List<Customer> theCustomers = customerDAO.getCustomers();
theModel.addAttribute("customers", theCustomers);
System.out.println(theCustomers);
return "list-customer";
}
}
类别:CustomerDAO实施
package com.luv2code.springdemo.DAO;
import com.luv2code.springdemo.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Repository
public class CustomerDAOImpl implements CustomerDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public List<Customer> getCustomers() {
Session getSession = sessionFactory.getCurrentSession();
Query<Customer> theCustomers = getSession.createQuery("from Customer", Customer.class);
List<Customer> customers = theCustomers.getResultList();
return customers;
}
}
类别:客户到数据库的映射
package com.luv2code.springdemo.entity;
import javax.persistence.*;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name = "email")
private String email;
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}
jsp页面。
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<!-- loop over and print our customers -->
<c:forEach var="tempCustomer" items="${customers}">
<tr>
<td> ${tempCustomer.firstName} </td>
<td> ${tempCustomer.lastName} </td>
<td> ${tempCustomer.email} </td>
</tr>
</c:forEach>
</table>
输出
答案 0 :(得分:0)
您可能正在使用旧版本的jstl,其中表达式必须称为witch <c:out/>
标记。
在jsp中尝试以下操作
<td><c:out value="${tempCustomer.firstName}" /></td>
<td><c:out value="${tempCustomer.lastName}" /></td>
<td><c:out value="${tempCustomer.email}" /></td>