java - 无法通过模型属性名称与表单属性名称相同的 GET 方法打开 JSP 表单页面
问题描述
当我打开 localhost:8080/customers/searchCustomer 时,它会引发异常:
org.springframework.beans.NotReadablePropertyException:bean 类 [java.lang.String] 的无效属性“ssn”:bean 属性“ssn”不可读或具有无效的 getter 方法:getter 的返回类型是否与参数类型匹配二传手?
我的 getter 和 setter 没问题。
我的实体:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private String surname;
private String ssn;
private int age;
private String emailAddress;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
在我的控制器中:
@Controller
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerService service;
// .....
@GetMapping("/searchCustomer")
public String searchCustomer(Model model) {
model.addAttribute("customerBySsnForm", new String());
return "searchCustomer";
}
@PostMapping("/showCustomer")
public String showCustomer(@ModelAttribute("customerBySsnForm") String ssn, Model model) {
Customer customer = service.getCustomerBySsn(ssn).get();
model.addAttribute("customerInShowCustomer", customer);
return "showCustomer";
}
}
在我的服务中:
public Optional<Customer> getCustomerBySsn(String ssn){
return repository.findBySsn(ssn);
}
我的存储库:
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, Long> {
public Optional<Customer> findBySsn(String ssn);
}
我的 searchCustomer.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h3>Search for customer</h3>
<form:form method="POST" action="showCustomer" modelAttribute="customerBySsnForm">
<table>
<tr>
<td><form:label path="ssn">Customer SSN</form:label></td>
<td><form:input path="ssn"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
</body>
</html>
编辑1:
我在控制器中的主要方法(用于客户插入)是:
@GetMapping("/addCustomer")
public String addCustomer(Model model) {
Customer customerToAdd = new Customer();
model.addAttribute("customerForm", customerToAdd);
return "addCustomer";
}
@PostMapping("/addCustomerResult")
public String addCustomerResult(@ModelAttribute("customerForm") Customer customer) {
service.addCustomer(customer);
return "addCustomerResult";
}
当我输入客户插入表格时,一切都很好。关于代码,我知道在 get 方法中创建的 Customer 对象,作为属性添加到 Model,将通过我在表单中传递的数据“填充”其字段。所以我需要一个“整个”客户对象。实际上,对于我遇到该错误的情况,尝试使用通过模型“传输”对象的相同方式,我想我需要向模型添加一个属性,其中只有一个字符串对象,该对象将在我插入字符串后设置进入 searchCustomer ssn 文本区域。我看到这让我犯了错误。
解决方案
推荐阅读
- r - 如何排除故障:RODBC 错误:状态 08001,代码 0,
- python - 从两个坐标和角恢复第三个坐标
- java - 如何返回带有回调的文档快照对象并实际上能够访问/将其传递给其他层?
- c++ - 一次性分配大量内存和逐步声明少量内存的区别
- ios - 什么是布局中的系统间距?
- php - laravel 问题,@foreach 和 var_dump 问题
- c++ - 声明一个静态结构并返回它的地址,而不是指针或引用?
- php - Opencart 期刊主题升级、移动应用程序和多供应商市场问题
- spring - Spring Boot 不使用 application.properties 文件上的杰克逊属性
- emacs - 为交互式 Common Lisp 开发正确加载文件