无法在Struts 2动作中加载参数

时间:2013-11-18 14:40:16

标签: java jsp parameters struts2 ognl

参考Struts 2 and business objects及其中所述的代码:

  • cannot load the rp.randomCode因为它给了null。我有getters/setters in both RequestParam class and the SendMessageOrStartChatAction。我可以加载(如果我使用旧代码,则获取请求param值)

  • 我无法在private fields中使用RequestParam class,因为如果我想rp.randomCode,则RequestParam类中的随机代码字段应为public。 (不想使用rp.getRandomCode())。现在,这不是很好,因为字段应该是私有的。

新代码:

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)){
           // ...........
        }

    }

}

旧代码:

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);       
        }

    }

}

如何使用上面的代码加载参数?

1 个答案:

答案 0 :(得分:0)

假设在JSP中有一个表单标记和一个或多个输入字段。每个字段都有name个属性。对于使用OGNL语法的Struts2映射表单字段到操作属性,此属性是不可思议的。像rp这样的嵌套bean应该用前缀rp.映射,因此在设置以dot命名的属性之前,OGNL将获得该bean的引用。要使其工作,请确保在操作配置中引用params拦截器,因为它负责填充操作实例的bean实例属性。