I’m using hibernate, Spring and Velocity in Java.
I’m extending the SimpleFormController and using a typical ‘User’ object for the command class.
I wish to update or create the User class using the form, which I reflect back into hibernate so that the values can be updated in the database.
My problem is that I wish to display additional data in the velocity template resolved to ‘userEdit’, but I don’t want to store this data within the user object.
My user object simplified is:
public class User implements Serializable {
private String username;
private String password;
private Set<Object> allowedNames;
...
}
My bean is defined as follows:
<bean id="userEditController" class="com.UserEditController">
<property name="sessionForm"><value>true</value></property>
<property name="commandClass"><value>com.data.User</value></property>
<property name="validator"><ref bean="userValidator"/></property>
<property name="formView"><value>userEdit</value></property>
<property name="successView"><value>UserEdit</value></property>
<property name="dao">
<ref bean="myDAO"/>
</property>
</bean>
The ‘allowedNames’ variable contains some of the list of Names in the database and is specific to the user. I am wondering how I can also display a full list of names (extracted from the database) so I can use it in my userEdit velocity template. Because I want to show the ‘allowedNames’ next to ‘allNames’.
Using the SimpleFormController allows me to directly control the User object, creating new Users or updating them in the database, however, adding any more information into the form is very difficult for me, because I’m stuck with the User object.
‘allNames’ does not belong in the User class so I have tried to create an object to hold the User object and a Set called allNames. I call it my UserEdit class. My problem there is that I cannot reference the User object variables without accessors in velocity template such as $command.user.username , and I also cannot bind them for errors or use the User object to reflect and form changes back into the database.
My question is in regards to how I can either user this ‘UserEdit’ object in my form or otherwise make use of a different approach in order that my form can have User data and other data and at the same time be able to update the User object(which is reflected in hibernate). Anyone have an approach for this?
What I’ve found out myself is that, indeed, the inherited code does not automatically update the commandClass User object through some sort of reflectance in spring.
What I’ve done to solve my problem is to creat a UserEdit object. This holds any necessary data coming from the form which I then place into the User object before saving it using my DAO.