Search code examples
javastruts2ognl

Populating Action class instance variables from URL in Struts2


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


Solution

  • 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.