gpt4 book ai didi

java - 无法通过模型属性名称与表单属性名称相同的 GET 方法打开 JSP 表单页面

转载 作者:行者123 更新时间:2023-12-02 09:12:25 27 4
gpt4 key购买 nike

当我打开 localhost:8080/customers/searchCustomer 时,它会引发异常:

org.springframework.beans.NotReadablePropertyException: Invalid property 'ssn' of bean class [java.lang.String]: Bean property 'ssn' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

我的 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 中:

@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:

Controller 中我的主要方法(用于客户插入)是:

@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 文本区域。我发现这让我犯了错误。

最佳答案

enter image description here

更改 model.addAttribute("customerBySsnForm", new String()); to model.addAttribute("customerBySsnForm", new Customer());

将@ModelAttribute("customerBySsnForm")字符串ssn更改为@ModelAttribute("customerBySsnForm")客户客户使用 customer.getSsn() 获取值

关于java - 无法通过模型属性名称与表单属性名称相同的 GET 方法打开 JSP 表单页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59303461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com