PDA

View Full Version : Form Controllers, Command Objects, and Validators


sbirnie
Jul 17th, 2007, 02:11 PM
I'm trying to create a page that handles the CRUD functions for a specific object. For example, a Person, which has name, address, phone, etc.

Several questions/issues:

MultiActionController - Seems like the logical type to use, but can it use validators and command objects?
If it can do validators, how do you set it up so it only validates certain objects based on the action being performed. Ex. For search, you only need to put in the last name so just validate it's not empty. But for adding a new Person, you need to validate all the fields.
Should I use multiple controllers? I'd like to have all 3 actions and their results available on a single page. Could the multiple controllers use the same successView?

Jörg Heinicke
Jul 17th, 2007, 08:07 PM
MultiActionController - Seems like the logical type to use, but can it use validators and command objects?

Yes. Have a look on the Javadoc (http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/multiaction/MultiActionController.html).

If it can do validators, how do you set it up so it only validates certain objects based on the action being performed. Ex. For search, you only need to put in the last name so just validate it's not empty. But for adding a new Person, you need to validate all the fields.

I guess that's not possible. I used to implement the search in an extra controller.

Should I use multiple controllers? I'd like to have all 3 actions and their results available on a single page. Could the multiple controllers use the same successView?


You need at least one controller per object class since the validators must be able to handle the object. Furthermore MAC is completely stateless, it does not know of any concept like successView.

Hope that helps,
Jörg

sbirnie
Jul 18th, 2007, 12:39 PM
I've created a MAC with 5 methods index, create, read, update, delete. I have not tried to implement validation yet. I figured I would add that after I got it working assuming the fields are valid.

The signatures are:
index(request, response) - for the first call to the page w/no paramaters.
create(request, response, createFormData)
read(request, response, readFormData)
update(request, response, updateFormData)
delete(request, response, deleteFormData)

I have 1 JSP with all 4 forms - one for each method. Here's an example of two of the forms, taking out most of the formatting.

<form:form method="post" name="createForm" action="/callToMACController">
<form:input path="field1" maxlength="20" size="20" />
<form:errors path="field1"/></td>
<form:input path="field2" maxlength="9" size="9" />
<form:errors path="field2"/>
<form:errors path="*"/>
<input type="submit" value="Add"/>
<input type="hidden" name="action" value="create" />
</form:form>

<form:form method="post" name="readForm" action="/callToMACController">
<form:input path="field3" maxlength="20" size="20" />
<form:errors path="field3"/></td>
<form:input path="field4" maxlength="9" size="9" />
<form:errors path="field4"/>
<form:errors path="*"/>
<input type="submit" value="Add"/>
<input type="hidden" name="action" value="read" />
</form:form>

<table... >
<c:if test="${!empty model.readEntries}">
<tr>Headers...</tr>
</c:if>
<c:forEach items="${model.readEntries}" var="current">
<tr>Row data...</tr>
</c:forEach>
</table>


When I try to go to the page for the first time, it uses index and just returns the view, but I get an error "Neither BindingResult nor plain target object for bean name 'command' available as request attribute".

Am I going about this all wrong?

Jörg Heinicke
Jul 18th, 2007, 08:47 PM
When I try to go to the page for the first time, it uses index and just returns the view, but I get an error "Neither BindingResult nor plain target object for bean name 'command' available as request attribute".

Command name of form:form defaults to command. You can specify it using attribute command on it. It has to point to the command object in your model.

Jörg