Some people call what you are doing master/detail screens. What I have been doing is creating two controllers, on for list and one for edit. For example, say you want to have a list of users and then be able to edit the users. Create a UsersController that extends AbstractController. In the handleRequestInternal method, get the list of users and store it in the model for the view. A good url mapping for this would be /users.do. Subsequent requests to the same url could include addtional parameters to sort, filter or page the list. Your code in handleRequestInternal should handle that. The first page might be /users.do, the second page /users.do?index=20&pageSize=20. The first request would use default values for index and pageSize, and the second request would use the values in the request parameters. You could also have parameters like sortProperty, sortOrder(asc|desc), etc.
The the second controller should be UserController and you want to extend SimpleFormController. The URL Mapping would be /user.do. You need to override two methods. First, override formBackingObject like this:
Code:
protected Object formBackingObject(HttpServletRequest request) throws Exception {
Long id = HTTPUtil.getLongParameter(request,"id");
User user = null;
if(id != null && id.longValue() > 0) {
return userService.get(id);
} else {
return new User();
}
}
This assumes that you have a userService with a get method that takes the primary key and gets the object from the database. If there is no id in the request, it is assumed you are doing an add, otherwise it is an edit. Then you can override onSubmit, where you can put your logic to create or update the record in the database. When you make an HTTP GET request to /user.do, formBackingObject is called, but onSubmit is not. When you do an HTTP POST (which happens when the HTML form is submitted), that is when onSubmit gets called.