Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Objects in Spring

I have a command object composed of primitive fields and an object field. How do I bind my form fields to the fields in the object?

I tried doing this but to no avail

<form:form commandName="course" method="POST">
     <form:input path="activity.activity"/>
         .
         .
         .
</form:form>

I get this error

org.springframework.beans.NotReadablePropertyException: 
      Invalid property 'course' of bean class

My Command class is like this

public class Course {
    private long id;
    private String owner;
    private String title;
    private List<LearningActivity> activity = new ArrayList<LearningActivity>();

    //getters and setters
}

public class LearningActivity {
private long ID;
private String activity;
    private String link;

    //getters and setters
}
like image 222
Jeune Avatar asked Sep 15 '09 21:09

Jeune


People also ask

What is spring command?

The Spring Boot CLI is a command line tool that you can use if you want to quickly develop a Spring application. It lets you run Groovy scripts, which means that you have a familiar Java-like syntax without so much boilerplate code. You can also bootstrap a new project or write your own command for it.

What is command class in Spring MVC?

Command Class is a normal Java Bean class, it can be instantiate by Spring WEB MVC framework inorder to store form data which is submitted by the respective Client along with request. The main intention to store form data in Command Class objects is. To make available form data to Business Logic to use.

What is model object in spring boot?

In Spring MVC, the model works a container that contains the data of the application. Here, a data can be in any form such as objects, strings, information from the database, etc. It is required to place the Model interface in the controller part of the application.

What is Webmvc?

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.


2 Answers

Your list either needs to be pre-populated with as many LearningActivity objects as you plan to refer to (using activity[0], activity[1], etc.) or it needs to be a lazy list. A lazy list is a list that will populate itself with empty objects when a given index is referenced.

A comment indicates that you're trying to use Apache Commons LazyList, which ought to work -- are you missing an import directive? However, as an alternative there is a Spring lazy list implementation called AutoPopulatingList.

like image 111
Jacob Mattison Avatar answered Nov 03 '22 05:11

Jacob Mattison


Two possible issues here:

  1. activity.activity is invalid (unless your getters do not correspond to your member variables) because Course.activity is a list. You need to address a particular list element - e.g. activity[0].activity. You'll also have to make sure it actually exists.

  2. Have you configured your FormController correctly? Does it pass Course instance to view as it should? Take a look at Spring MVC tutorial for an example.

If after you've fixed #1 and verified that #2 is done correctly the error doesn't go away, please post more details (FormController mapping / source).

like image 30
ChssPly76 Avatar answered Nov 03 '22 07:11

ChssPly76