Question

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:

  1. Go to http://localhost/Vikos/secure/user.
  2. You will be routed to a login page. Enter: logon email = consumer@test.com and password = password.
  3. 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.

Was it helpful?

Solution

You have incorrectly mapped the form to the user action. The user action is mapped to the default namespace but the form is mapped to /modelDriven namespace. So, when you submit the form you should get 404 error. Remove the namespace attribute from the form tag which should be a struts tag. If you don't set the namespace attribute of the form tag the action might use the default namespace. More about namespaces and its configuration you could find here. The code to replace

<s:form accept-charset="UTF-8" action="user" method="POST" theme="simple">

Note, added simple theme for the form to not affect the HTML design. Also, if you are running in developer mode the warnings should disappear if the action in the struts form tag is mapped correctly.

Also note, that strings in Java are compared with the equals method, not ==. So, the crudType variable might not change and it prevents the update operation.

OTHER TIPS

Firstly

I have some issues with your jsp. Why are you not using struts2 tag libraries (s:textfield) for your input. See if you can change that. Also add namespace="/" to your form element.

Secondly

Change UserModel Constructor from

public class UserModel extends BaseModel {    
    private UserVo userVo;      
    public UserModel() {
        super();
    }
}

to

public class UserModel extends BaseModel {    
    private UserVo userVo;      
    public UserModel() {
        super();
            userVo = new UserVo();
    }
}

Thirdly

Get your BaseModel to implement java.io.Serializable i.e.

public abstract class BaseModel implements java.io.Serializable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top