Ниже указан мой код контроллера:
@Controller
public class CustomerController {
@Autowired
private CustomerService service;
@GetMapping("/findCustomer")
public String findCustomer(Customer customer, Model model) {
List<Customer> customers = service.findCustomers(customer);
model.addAttribute("customers", customers);
return "viewCustomer";
}
}
Ниже указан мой jsp код:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h2>Customer Details</h2>
<table>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>Vehicle Number</th>
<th>Address</th>
<th>Bank</th>
<th>GST Number</th>
</tr>
<c:forEach var="cust" items="${customers}">
<tr>
<td>${cust.name}</td>
<td>${cust.phoneNumber}</td>
<td>${cust.vehicleNumber}</td>
<td>${cust.address}</td>
<td>${cust.bank}</td>
<td>${cust.gstNumber}</td>
</tr>
</c:forEach>
</table>
<c:forEach items="${customers}" var="cust">
<p>${cust.name}</p><br/>
</c:forEach>
</body>
</html>
Ниже приведен мой класс сущности :
@Data
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long customerId;
private String name;
private Long phoneNumber;
private String vehicleNumber;
private String address;
private String bank;
private String gstNumber;
}
Когда я отправляю запрос следующему:
http://localhost:8080/findCustomer?name=XYZ&phoneNumber=216217638
Контроллер отправляет ответ на jsp. Я могу видеть значения в items="${customers}"
, но следующее не дает результата
<c:forEach var="cust" items="${customers}">
<tr>
<td>${cust.name}</td>
<td>${cust.phoneNumber}</td>
<td>${cust.vehicleNumber}</td>
<td>${cust.address}</td>
<td>${cust.bank}</td>
<td>${cust.gstNumber}</td>
</tr>
</c:forEach>