2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Todd F
  • 35
  • 9
  • Post a full stacktrace, it looks like warning, but the values are set to the model not the action. – Roman C Jan 20 '14 at 12:01

2 Answers2

0

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Roman, thanks for all the analysis. I've made the changes you recommended, but the issue still persists. I had said the values weren't getting sent back, but they are. The issue looks like Struts2 is attempting to load the UserModel with the request parameter values that aren't fully resolved (see updates above). – Todd F Jan 19 '14 at 19:45
  • You should use input values in quotas in the JSP. – Roman C Jan 19 '14 at 20:34
  • Also remove the prepare method, it's buggy! Use a separate action per operation. – Roman C Jan 19 '14 at 20:43
  • It's looking like I need to implement a custom type converter. I've updated the code with the bones of a UserTypeConverter, but I haven't been able to get it to fire at this point. – Todd F Jan 20 '14 at 01:38
  • Roman, after I got all the other issues sorted out the issue persisted because the prepare() method was being called prior to the Model getting populated. You were right, I needed to remove the preparable interface. The entire thing isn't working, but I am passed that foundational issue. Thanks for all your help. – Todd F Jan 22 '14 at 04:18
0

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
Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
  • Uchenna, I applied all the changes that you recommended. The ParametersInterceptor errors went away when I instantiated the UserVo inside the UserModel's constructor which makes sense now that I see it was never getting instantiated. The userModel still isn't getting populated with the POSTed form values. I'll upload my changes and keep playing with this. Any other thoughts are greatly appreciated. Thanks, Todd – Todd F Jan 21 '14 at 22:48
  • Uchenna, Thank you for your help. Between the advice that Roman and you gave it looks like things are moving forward at this point. Much appreciated. Thank you. – Todd F Jan 22 '14 at 04:21