I am attempting to create a dynamic web application using the ModelDriven
interface with Struts 2. I have worked with Struts1, but this is my first attempt at Struts 2.
I am able to populate a form from the values stored in the database, but when I modify the values and attempt to update the database, the form values aren't being loaded into the Model.
I have uploaded the entire project here (it does require a MySQL database to run against at this point).
Once you have the project loaded:
- Go to
http://localhost/Vikos/secure/user
. - You will be routed to a login page. Enter:
logon email = consumer@test.com
andpassword = password
. - You will be taken to a
User
page with a form on it populated from the database. If you update the Last Name field and hit the<Update>
button, it should update the value in the database, but it isn't passing the value back.
How to get the values from the HTML form into the Model?
UPDATE:
I retitled this post and modified the contents a little to more accurately describe what is occurring. I have also added some debug code to UserAction.prepare()
method. It looks like Struts2 is attempting to load the UserModel
properties with the String array (parmValues
) as opposed to the 1st element of the parameter array (correctVal
).
public void prepare() throws Exception {
// BEGIN - ADDED FOR DEBUGGING PURPOSES
Map parameters = request.getParameterMap();
Set keys = parameters.keySet();
Iterator iter = keys.iterator();
String key;
String[] parmValues;
while (iter.hasNext()) {
key = iter.next().toString();
parmValues = (String[])(String[])parameters.get(key);
if (parmValues.length == 1) {
String correctVal = parmValues[0];
System.out.println(correctVal); // This is printing out the correct updated form values.
}
}
// END - ADDED FOR DEBUGGING PURPOSES
determineCrudType();
}
13:21:42.773 [http-bio-80-exec-7] ERROR c.o.x.i.ParametersInterceptor
- Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'email' on 'class com.codedeart.vikos.action.UserAction: Error setting expression 'email' with value '[Ljava.lang.String;@7e227c19'
13:21:42.775 [http-bio-80-exec-7] ERROR c.o.x.i.ParametersInterceptor
- Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'firstName' on 'class com.codedeart.vikos.action.UserAction: Error setting expression 'firstName' with value '[Ljava.lang.String;@378da3ff'
13:21:42.776 [http-bio-80-exec-7] ERROR c.o.x.i.ParametersInterceptor
- Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'lastName' on 'class com.codedeart.vikos.action.UserAction: Error setting expression 'lastName' with value '[Ljava.lang.String;@3409d6d7'
13:21:42.778 [http-bio-80-exec-7] ERROR c.o.x.i.ParametersInterceptor
- Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'middleName' on 'class com.codedeart.vikos.action.UserAction: Error setting expression 'middleName' with value '[Ljava.lang.String;@13b29db7'
UPDATE2:
It looks like this issue is similar to ones reported by others who have fixed it using Custom Type Converters. I have attempted to implement a custom type converter based on this tutorial. I haven't gotten to the point of actually doing any type conversion yet as the methods on my UserTypeConverter
are not firing. I have uploaded the latest code to BitBucket.
P.S.:
I had issues with my JSP and backend Struts implementation. The answer is to follow the advice provided by both Uchenna and Roman below. I will keep updating my project in BitBucket so hopefully no one else need make these mistakes.