2017-12-12 8 views
0

私はSpring MVCプロジェクトに取り組んでいます。チェックボックスの値をバックエンドファイルにバインドする際にエラーが発生します。いずれか1つは間違いや欠けているステップは何を示唆してくださいことができますか?SPRING MVCフォーム:checboxesバインディングエラー

私は類似のスレッドを検索しましたが、答えが私の問題を解決しませんでした。ここで何か助けてくれてありがとう。

コード:

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%> 
<%@ page session="false" %> 
<html> 
<head> 
<title>Home</title> 
</head> 
<body> 

<form:form modelAttribute="listOfShapes" action="/calculateArea">  
<form:checkboxes items="${listOfShapes}" path="selectedShapes" /> 
</form:form> 

</body> 
</html> 

MOdelAttribute:

@ModelAttribute("listOfShapes") 
    public List<String> getListOfShapes() { 
     List<String> shapesOfList = new ArrayList<String>(); 
     shapesOfList.add("Circle"); 
     shapesOfList.add("Rectangle"); 
     shapesOfList.add("Square"); 
     return shapesOfList; 
    } 

POJO:

package model.pojo.org; 

import java.util.List; 

public class ListOfShapes { 

private List<String> selectedShapes; 

/** 
* @return the selectedShapes 
*/ 
public List<String> getSelectedShapes() { 
    return selectedShapes; 
} 

/** 
* @param selectedShapes the selectedShapes to set 
*/ 
public void setSelectedShapes(List<String> selectedShapes) { 
    this.selectedShapes = selectedShapes; 
} 



} 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<context:component-scan base-package="calculate.area.org" /> 

</beans:beans> 

私は以下のエラーを取得しています。誰でもお勧めしますか?

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

答えて

0

フォームモデルはlistOfShapesであってはなりません。フォームモデルは実際に入力を受け取ります。だから私は、これは間違っていると信じて:

<form:form modelAttribute="listOfShapes" action="/calculateArea"> 

ディスプレイの定数は、あなたの<form:checkboxes items="${listOfShapes}" path="selectedShapes" />ゲッターチェックボックスとして使用するにはOKですが、それは入力を収集し、実際のフォームモデルとしてそれを使用するには、[OK]ではありません。これはあなたのPOJOインスタンスであり、定数のリストではありません。

例えば、ここを参照してください: http://www.baeldung.com/spring-mvc-form-tutorial

を私は右、あなたがフォームのモデルオブジェクトとのModelAndViewを公開コントローラーを持っていると仮定?

public String processForm(@ModelAttribute("employee")Employee employee, 
     BindingResult result, ModelMap model) { 
} 

のようなもの、その場合にはフォームモデルは、そのウェブサイトの例のように

<form:form method="POST" action="/spring-mvc-xml/addEmployee" modelAttribute="employee"> 

だろう。

関連する問題