Research Log of Web Science Students

Computer Science is not simply programming

Dynamic List binding in Spring Forms

with one comment

This is the answer to our previous problem. However, I had realized immediately we can’t use this after all (javascript can’t add new forms to spring forms dynamically since the jsp is server side thing and javscript ain’t):

JSP runs on the server, Javascript (which is NOT Java) runs on the client.

So this is trivial right now but still good knowledge to pass around since there aren’t a lof of blog posts about it.

The problem: You want to bind a list of objects to a command class using Spring Form Tags and the Spring SimpleFormController.

An Example domain class:

public class Course {
	private long id;
	private String owner;
	private String title;
	private List<String> learningObjectives;
	private List<LearningActivity> activity=
                LazyList.decorate(new ArrayList<LearningActivity>(), new
                InstantiateFactory(LearningActivity.class)); ;
        //accessor methods

The key here is LazyList from the jakarta commons-collections.
Lazylist lazily instantiates a new instance of learning activity every for every input text box in your html/jsp that needs it.

Setup the SimpleFormController. Ours is simple with the formViews (the page that displays the form) and the successView(the page that shows on a successful form input) set in the xml file already:

	<bean name="/" class="com.springtutorial.controller.UsingSimpleFormController">
		<property name="formView" value="course-form"/>
		<property name="successView" value="course-form-success"></property>

Of course code your spring controller:

public class UsingSimpleFormController extends SimpleFormController {
	public UsingSimpleFormController(){

	protected ModelAndView onSubmit(Object command) throws Exception {
		Course course = (Course) command;
		return super.onSubmit(course);

The command class is the domain model and also the command class to which you will bind your form fields to.

In the jsp:

<%@ taglib uri="" prefix="form"%>
<form:form commandName="course" method="POST">
       Course Title <form:input path="title"/> <br/><br/>
       Learning Objective <br/>
       <form:input path="learningObjectives"/> <br/><br/>
       <form:input path="learningObjectives"/> <br/><br/>
       <form:input path="learningObjectives"/> <br/><br/>

        <form:input path="activity[0].activity"/>
        <form:input path="activity[1].activity"/>
        <form:input path="activity[2].activity"/>

<input type="submit"/>

Written by Jose Asuncion

September 17, 2009 at 6:25 pm

Posted in Hardwire

One Response

Subscribe to comments with RSS.

  1. […] through a feature in the past that required a lot of learning of the Spring framework (see here, here, here . And today I found myself going through the same […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: