Expert Spring MVC and Web Flow

(Dana P.) #1
<option>${language}</option>
</c:forEach>
</select>
</td>
</tr>

Of course, it’s common to return extra information to the Viewafter a form submission.
You may, for example, want to provide a confirmation message after the submission com-
pletes. In our example so far, we have implemented the doSubmitAction()method, which does
not allow for any model to be returned. Again, the doSubmitAction()method is useful when
the default success view is sufficient. When you need to return objects required by the success
view, you will need to implement an onSubmit()method.
For example, we will extend our PersonFormControllerto persist the person instance into
the database, and we will recommend a book to read based on their favorite programming
language. We will remove our doSubmitAction()method and override onSubmit()so that we
may return a model with the book recommendation, as shown in Listing 6-47.

Listing 6-47.PersonFormController with onSubmit()

public class PersonFormController extends SimpleFormController {

private String[] languages = new String[]{"Java", "Ruby", "Python"};

private PersonDao personDao;

public PersonFormController() {
setCommandName("person");
setCommandClass(Person.class);
setFormView("newPerson");
setSuccessView("newPersonSuccess");
}

public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

@Override
protected Map<String, String[]> referenceData(HttpServletRequest req)
throws Exception {
Map<String, String[]> data = new HashMap<String, String[]>();
data.put("languages", languages);
return data;
}

162 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf