Search code examples
javajspparametersstruts2ognl

Cannot load parameters in Struts 2 action


In reference to Struts 2 and business objects and the code stated there:

  • I cannot load the rp.randomCode as it gives it null. I have getters/setters in both RequestParam class and the SendMessageOrStartChatAction. I can load (get the request param value if I use the old code though)

  • I cannot use private fields in the RequestParam class because if I want to do rp.randomCode, the random code field in the RequestParam class should be public. (dont want to use rp.getRandomCode() though). Now, this is not good as fields should be private.

new code:

public class RequestParam{
    private int userId;
    private int groupType;
    private int groupId;
    private String groupTitle;
    private String groupMemberIds;
    private int randomCode;
    private String message;
    private int messageId; //internal class ues
    //public getters and setters here

}

public class SendMessageOrStartChatAction extends BaseActoinSupport{

    private RequestParam rp;
    //public getters and setters here

    @Override
    protected void doExecute() throws IOException {

        //check if it had random code in db, (msg already saved in db)
        if(ChatDao.randomCodeExists(rp.randomCode)){
           // ...........
        }

    }

}

old code:

public class SendMessageOrStartChatAction extends BaseActoinSupport{

    private static final long serialVersionUID = 1L;
    private int userId;
    private int groupType;
    private int groupId;
    private String groupTitle;
    private String groupMemberIds;
    private int randomCode;
    private String message;
    private int messageId; //internal class ues

    @Override
    protected void doExecute() throws IOException {

        //check if it had random code in db, (msg already saved in db)
        if(ChatDao.randomCodeExists(randomCode)){
            messageId = ChatDao.getMessageIdThatMatchesRandomCode(randomCode);  
            write(messageId);

        }else{
            if(groupId <= 0){
            //create group 
                groupId = ChatDao.createChatGroup(userId, groupTitle, groupType);
                String[] memberIdsArray = groupMemberIds.split("=="); 
                ChatDao.addUsersToGroup(groupId, memberIdsArray);
            }
            //save message
            messageId = ChatDao.saveMessage(userId,groupId , message);
            // queued: may be put this in last viewed messages here. may be.
            write(messageId);       
        }

    }

}

How to load parameters using the code above?


Solution

  • Suppose in the JSP you have a form tag and one or more input fields. Each field has a name attribute. This attribute is magic for Struts2 maps form fields to action attributes using OGNL syntax. Nested beans like rp should be mapped with prefix rp., so OGNL will get the reference of that bean before setting properties named after dot. To make it working make sure the params interceptor is referenced in the action configuration, because it's responsible for populating bean instance properties of your action instance.