Bean property is not readable or has an invalid getter method:

我有一个jsp文件如下,我用这个jsp文件来储存用户输入的Stock信息。每一个input,我都用spring form tag绑定这个对象/bean的某个属性。

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>add Stock</h1>

        <form:form action="addStock" modelAttribute="stock" method="POST">
        
            <table>
                <tbody>
                
                    <tr>
                        <td><label>Base Rate Of Return:</label></td>
                        <td><form:input path="baseRateOfReturn" /></td>
                    </tr>
                
                    <tr>
                        <td><label>Asset Code:</label></td>
                        <td><form:input path="code" /></td>
                    </tr>
                
                    <tr>
                        <td><label>Asset Label:</label></td>
                        <td><form:input path="label" /></td>
                    </tr>

                    <tr>
                        <td><label>Quarterly Dividend:</label></td>
                        <td><form:input path="quarterlyDividend" /></td>
                    </tr>
                    

                    
                    <tr>
                        <td><label>Price:</label></td>
                        <td><form:input path="sharePrice" /></td>
                    </tr>
                    
                    <tr>
                        <td><label>Beta:</label></td>
                        <td><form:input path="beta" /></td>
                    </tr>
                    
                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Save"/></td>
                    </tr>

                
                </tbody>
            </table>
        
        
        </form:form>
        


</body>
</html>

现在我却遇到一个exception。这个异常的关键信息如下。

Invalid property 'code' of bean class [pojo.Stock]: Bean property 'code' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

我返回去看我的Stock class,发现这个class确实没有定义code的getter和setter method。如下:

package pojo;

public class Stock extends Investment {
    private Double sharePrice;
    private Double beta;// risk measurement beta

    public Stock() {
    }

    public Stock(String code, String label, Double quarterlyDividend, Double baseRateOfReturn, Double sharePrice,
            Double beta) {
        super(code, label, quarterlyDividend, baseRateOfReturn);
        this.sharePrice = sharePrice;
        this.beta = beta;
    }


    
    public Double getSharePrice() {
        return sharePrice;
    }

    public void setSharePrice(Double sharePrice) {
        this.sharePrice = sharePrice;
    }

    public Double getBeta() {
        return beta;
    }

    public void setBeta(Double beta) {
        this.beta = beta;
    }

    @Override
    public String accountType() {
        return "S";
    }

    @Override
    public Double riskMeasure() {
        return Math.round(100 * beta) / 100d;
    }


}

关键在于,Stock是Investment的子类,code的getter和setter方法既然已经在Investment定义了,就不应该还在子类Stock重写。

那么在不重写getCode和setCode方法的前提下,如何可以解决这个异常呢?

下面是父类Investment及其父类Asset的pojo class。

package pojo;

public abstract class Investment extends Asset {
    private Double quarterlyDividend;
    private Double baseRateOfReturn;

    public Investment() {
        

    }

    public Investment(String code, String label, Double quarterlyDividend, Double baseRateOfReturn) {
        super(code, label);
        this.quarterlyDividend = quarterlyDividend;
        this.baseRateOfReturn = baseRateOfReturn;

    }

    public Double getQuarterlyDividend() {
        return quarterlyDividend;
    }

    public void setQuarterlyDividend(Double quarterlyDividend) {
        this.quarterlyDividend = quarterlyDividend;
    }

    public Double getBaseRateOfReturn() {
        return baseRateOfReturn;
    }

    public void setBaseRateOfReturn(Double baseRateOfReturn) {
        this.baseRateOfReturn = baseRateOfReturn;
    }

    public abstract Double riskMeasure();

    public abstract String accountType();

}
package pojo;



public abstract class Asset {
    private Integer assetId;
    private String code;
    private String label;
    
    public Asset() {
    }
    
    public Asset(String code, String label) {
        this.code = code;
        this.label = label;
    }

    public abstract Double riskMeasure();
    public abstract String accountType();
    
    public Integer getAssetId() {
        return assetId;
    }
    
    public void setAssetId(Integer assetId) {
        this.assetId = assetId;
    }
    
    protected String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}
阅读 9.7k
1 个回答
 protected String getCode() {
        return code;
    }
code属性的get方法有问题,权限修饰符为public试试
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题