1

Facing an issue with passing values from my html form to action class. Created a sample project to test the functionality and have the same issue here. I have the following classes:

TestBean

package com.struts2test.beans;

public class TestBean {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

TestBeanHolder

package com.struts2test.beans;

import java.util.List;
import java.util.Map;

public class TestBeanHolder {
    private Map<Integer, TestBean> testBeanMap;
    private List<TestBean> testBeanList;
    private Map<Integer, List<TestBean>> testBeanListMap;

    public Map<Integer, TestBean> getTestBeanMap() {
        return testBeanMap;
    }
    public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
        this.testBeanMap = testBeanMap;
    }
    public Map<Integer, List<TestBean>> getTestBeanListMap() {
        return testBeanListMap;
    }
    public void setTestBeanListMap(Map<Integer, List<TestBean>> testBeanListMap) {
        this.testBeanListMap = testBeanListMap;
    }
    public List<TestBean> getTestBeanList() {
        return testBeanList;
    }
    public void setTestBeanList(List<TestBean> testBeanList) {
        this.testBeanList = testBeanList;
    }
}

TestAction

package com.struts2test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2test.beans.TestBeanHolder;

public class TestAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private TestBeanHolder testBeanHolder;

    public TestBeanHolder getTestBeanHolder() {
        return testBeanHolder;
    }

    public void setTestBeanHolder(TestBeanHolder testBeanHolder) {
        this.testBeanHolder = testBeanHolder;
    }

    public String execute() throws Exception {
        return SUCCESS;
    }
}

When my url is http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanMap[0].value=1, testBeanHolder.testBeanMap of my action gets populated with key of 0 mapping to a TestBean instance with value=1.

When the url is http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1, testBeanHolder.testBeanList gets populated with single instance of TestBean with value=1.

I am try to populate testBeanListMap property of testBeanHolder and doesn't work. The testBeanListMap is created but empty. Here is the URL I am trying http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1

Roman C
  • 49,761
  • 33
  • 66
  • 176
James Jithin
  • 10,183
  • 5
  • 36
  • 51

3 Answers3

1

Here is the code which worked, adding modified classes:

TestBeanListHolder

package com.struts2test.beans;

import java.util.List;

public class TestBeanListHolder {
    private List<TestBean> testBeans;

    public List<TestBean> getTestBeans() {
        return testBeans;
    }

    public void setTestBeans(List<TestBean> testBeans) {
        this.testBeans = testBeans;
    }

}

TestBeanHolder

package com.struts2test.beans;

import java.util.List;
import java.util.Map;

public class TestBeanHolder {
    private Map<Integer, TestBean> testBeanMap;
    private List<TestBean> testBeanList;
    private Map<Integer, TestBeanListHolder> testBeanListMap;

    public Map<Integer, TestBean> getTestBeanMap() {
        return testBeanMap;
    }

    public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
        this.testBeanMap = testBeanMap;
    }

    public Map<Integer, TestBeanListHolder> getTestBeanListMap() {
        return testBeanListMap;
    }

    public void setTestBeanListMap(
            Map<Integer, TestBeanListHolder> testBeanListMap) {
        this.testBeanListMap = testBeanListMap;
    }

    public List<TestBean> getTestBeanList() {
        return testBeanList;
    }

    public void setTestBeanList(List<TestBean> testBeanList) {
        this.testBeanList = testBeanList;
    }
}

URL

http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[1].testBeans[0].value=somevalue

James Jithin
  • 10,183
  • 5
  • 36
  • 51
0

The url http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1 won't work because you are using wrong parameter name. Thus testBeanHolder.testBeanListMap[0][0].value is a name of the parameter that maps to the object which has a property of complex type (collection of collections). Struts2 can't handle such scenarios, . But you can wrap a second collection with an object and use a collection of objects. The name would change to testBeanHolder.testBeanListMap[0].object[0].value.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks @Roman. It worked. Needed a holder for `List` – James Jithin Oct 07 '15 at 10:16
  • @JamesJithin have you read [the answer](http://stackoverflow.com/a/15009137/1654265) linked in my comment ? Roman, why saying *not sure if struts can handle such scenarios* if we know since that Q&A that it can't ? – Andrea Ligios Oct 07 '15 at 15:23
  • He's asking you a question directly related to your answer; opening a new question to reply to your answer would be inappropriate. – Dave Newton Oct 07 '15 at 19:02
  • ... he asked why you said it. I don't see what the issue is. The purpose of asking a question is to get an answer--not just to answer it yourself, no? Not having an answer seems like the *perfect* time to ask a question. – Dave Newton Oct 07 '15 at 19:58
  • *I* wasn't asking why. – Dave Newton Oct 07 '15 at 20:52
-1

The expression testBeanHolder.testBeanListMap[0][0].value is not a valid OGNL expression.

See here for a complete reference of what is allowed.

sh0rug0ru
  • 1,596
  • 9
  • 9