简体   繁体   中英

SPRING MVC form:checboxes binding error

I am working on Spring MVC project. I am getting an error while binding the checkbox value to the back end file. Can any one please suggest whats the mistake or missing step please?

I have searched for similar threads but the answers did'nt solve my issue. Appreciate any help here.

Code:

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>

I am getting the below Error. Can anyone please suggest?

 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?

The Form Model should not be listOfShapes . The Form Model is what actually receives the input. So I believe this is wrong:

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

It's OK to use it as a Checkbox Display Constants getter in your <form:checkboxes items="${listOfShapes}" path="selectedShapes" /> but it's not OK to use it as an actual Form Model that gathers input. That would be your POJO instance, not a list of constants.

For example, see here: http://www.baeldung.com/spring-mvc-form-tutorial

I assume you have a Controller that exposes the ModelAndView with the form model object, right? Something like

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

in that case the Form Model would be

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

as in that website example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM