2016-10-02 68 views
0

我得到一個錯誤:無效的屬性bean類的 'redeemVoucherForm'[my.testapp.forms.RedeemVoucherForm]

Invalid property 'redeemVoucherForm' of bean class [my.testapp.forms.RedeemVoucherForm]: Bean property 'redeemVoucherForm' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 

RedeemVoucherForm.java:

package my.testapp.forms; 

public class RedeemVoucherForm { 

    private String voucherCode; 

    public String getVoucherCode() { 
     return voucherCode; 
    } 

    public void setVoucherCode(String voucherCode) { 
     this.voucherCode = voucherCode; 
    } 
} 

在我PageController.java,我當我調用方法model.containsAttribute(「redeemVoucherForm」)時,存在添加模型redeemVoucherForm

@RequestMapping(method = RequestMethod.GET) 
public String showPage(final Model model) { 
    ... 
    model.addAttribute("redeemVoucherForm", new RedeemVoucherForm()); 
    ... 
    return REDIRECT_PAGE_URL; 
} 

@RequestMapping(value = "/redeem-voucher", method = RequestMethod.POST) 
public String redeemVoucher(@Valid final RedeemVoucherForm redeemVoucherForm, final BindingResult bindingResult, final RedirectAttributes redirectModel, final Model model, HttpServletRequest request) { 
    LOG.debug("<POST> Redeeming voucher..."); 

    return REDIRECT_PAGE_URL; 
} 

pageDisplay.jsp:

<c:url value="/page/redeem-voucher" var="redeemVoucherAction" /> 
<form:form action="${redeemVoucherAction}" method="post" commandName="redeemVoucherForm"> 
     <form:input cssClass="form-control" type="text" path="redeemVoucherForm.voucherCode"/> 
     <button type="submit" class="btn btn-primary btn-block checkoutButton"> 
      <spring:theme text="Redeem Voucher"/> 
     </button> 
</form:form> 

我還有什麼可會丟失,當我已經設置模式redeemVoucherForm

回答

2

我相信這條線

<form:input cssClass="form-control" type="text" path="redeemVoucherForm.voucherCode"/> 

應該

<form:input cssClass="form-control" type="text" path="voucherCode"/> 

一個<form:input>path元素應該是相對於你已經設置爲您<form:form>commandName的對象。在你的情況下,這個對象是你的類RedeemVoucherForm的一個實例。

如果將路徑設置爲redeemVoucherForm.voucherCode,Spring將在您的類RedeemVoucherForm上查找redeemVoucherForm屬性。它期望這個值具有voucherCode屬性,可以從中讀取表單值。當然,redeemVoucherForm屬性不存在,因此你會得到錯誤。

+0

'redeemCode'?這必須在你的代碼中,但它不會出現在你發佈的代碼中的任何地方。 –

+0

我的不好,我再次輸入錯誤。我寫了redeemCode而不是voucherCode。一切都好。非常感謝你! – Kapparino

相關問題