Here's the relavant part of the DispatcherServlet configuration file:
Code:
<beans>
...
<bean id="userEditValidator" class="bus.UserEditValidator"/>
<bean id="userEditForm" class="web.UserEditFormController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value>user</value>
</property>
<property name="commandClass">
<value>bus.User</value>
</property>
<property name="validator">
<ref bean="userEditValidator"/>
</property>
<property name="formView">
<value>user-edit</value>
</property>
<property name="successView">
<value>user-management</value>
</property>
<property name="userManager">
<ref bean="userManager"/>
</property>
</bean>
...
</beans>
Here's the code for the Controller (fairly bare until I resolve this problem):
Code:
public class UserEditFormController extends SimpleFormController
{
private UserManager userManager;
protected ModelAndView onSubmit(Object command) throws Exception
{
User user = (User)command;
return new ModelAndView(getSuccessView());
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception
{
String username;
User user;
username = request.getParameter("username");
if (username != null)
{
user = userManager.getUser(username);
if (user == null)
{
// username not found so create a new user with the given name
user = new User();
user.setUsername(username);
}
return user;
}
// no username parameter found so just create an empty, default user
return new User();
}
public UserManager getUserManager()
{
return userManager;
}
public void setUserManager(UserManager userManager)
{
this.userManager = userManager;
}
}
And the validator:
Code:
public class UserEditValidator implements Validator
{
public boolean supports(Class clazz)
{
return clazz.equals(User.class);
}
public void validate(Object obj, Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors,
"username",
"user-edit-error.username-required",
"Username required");
}
}
Thanks,
Peeper.