6. 使用SSM框架整合技术完成按照id查询数据并在custormer.jsp页面中显示,程序功能如图所示。已有类Customer,这个类有成员变量id、username、jobs、phone;已有customerService.findCustomerById(id)可以完成查询并返回Customer类型的查询结果。完成控制器中的findCustomerById方法,和custormer.jsp
时间: 2024-03-10 10:50:35 浏览: 22
好的,我来说明一下。
首先,在控制器中,需要注入customerService,然后实现findCustomerById方法,代码如下:
```
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/findCustomerById")
public String findCustomerById(String id, Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute("customer", customer);
return "customer";
}
}
```
在上面的代码中,我们使用了@Autowired注解来注入CustomerService,然后实现了findCustomerById方法,该方法接收一个id参数,并调用CustomerService的findCustomerById方法来查询对应的Customer对象。查询结果存储在Model中,并返回"customer"字符串,表示跳转到customer.jsp页面。
接下来,我们来实现customer.jsp页面。代码如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Customer</title>
</head>
<body>
<table>
<tr>
<td>id:</td>
<td>${customer.id}</td>
</tr>
<tr>
<td>username:</td>
<td>${customer.username}</td>
</tr>
<tr>
<td>jobs:</td>
<td>${customer.jobs}</td>
</tr>
<tr>
<td>phone:</td>
<td>${customer.phone}</td>
</tr>
</table>
</body>
</html>
```
在上面的代码中,我们使用了JSTL标签库来展示Customer对象的信息,其中${customer.id}、${customer.username}、${customer.jobs}、${customer.phone}分别表示Customer对象的id、username、jobs、phone属性。注意,我们在控制器中存储查询结果时,使用了"customer"作为键值,因此在JSP中,需要使用${customer}来获取查询结果。
至此,按照id查询数据并在custormer.jsp页面中显示的功能就实现了。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)