首页 > 解决方案 > 无法通过模型属性名称与表单属性名称相同的 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 文本区域。我看到这让我犯了错误。

标签: javaspringjsp

解决方案


在此处输入图像描述

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

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


推荐阅读