无法使用模型属性名称与表单名称相同的GET方法打开JSP表单页面

时间:2019-12-12 11:21:45

标签: java spring 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)将通过我在表单中传递的数据“填充”其字段。因此,我需要一个“整个” Customer对象。 确实,对于我遇到该错误的情况,尝试通过模型使用与“运输”对象相同的方式,我想我需要为Model添加一个属性,该属性仅包含一个在插入字符串后设置的String对象进入searchCustomer ssn文本区域。 我看到这让我出错了。

2 个答案:

答案 0 :(得分:1)

enter image description here

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

将@ModelAttribute(“ customerBySsnForm”)字符串ssn更改为@ModelAttribute(“ customerBySsnForm”)客户客户 使用customer.getSsn()获得价值

答案 1 :(得分:0)

发布问题时,我没有意识到Model类仅存储旨在渲染视图的对象。我终于解决了我的问题。

@GetMapping("/searchCustomer")
public String searchCustomer(Model model) {
    model.addAttribute("customerBySsnForm", new Customer());
    return "searchCustomer";
}

@PostMapping("/showCustomer")
public String showCustomer(@ModelAttribute("customerBySsnForm") Customer customer, Model model) {
    String ssn = customer.getSsn();
    customer = service.getCustomerBySsn(ssn).get();
    model.addAttribute("customerInShowCustomer", customer);
    return "showCustomer";
}