0

I have an iterator which requires to use mapped properties or indexed properties but my getter-setter are not getting those values.

For Ex: (This is just an example. Ultimately the idea is whether I can use mapped property in struts 2 or not. If yes, then how.)

index.jsp:

<s:form action="hello" namespace="foo">    
    <s:textfield name="arp(0)" /> <br/>
    <s:textfield name="prp(0)" /> <br/>
    <s:textfield name="arp(1)" /> <br/>
    <s:textfield name="prp(1)" /> <br/>
    <s:submit   value="Say Hello" /> 
</s:form>

helloWorld.action:

class PRLists {
    String arp;
    String prp;

    public String getArp() {
        return Arp;
    }

    public void setArp(String aRP) {
        arp = aRP;
    }

    public String getPrp() {
        return prp;
    }

    public void setPrp(String pRP) {
        prp = pRP;
    }

}

public class HelloWorldAction {
    ArrayList<PRLists> prlist = new ArrayList<PRLists>();

    public String execute() throws Exception {
        System.out.println("ruuning execute");
        return "success";
    }

    public ArrayList<PRLists> getPrlist() {
        return prlist;
    }

    public void setPrlist(ArrayList<PRLists> prlist) {
        this.prlist = prlist;
    }

    public String getArp(String key) {
        int index = Integer.parseInt(key);
        return prlist[index].arp;
    }

    public void setArp(String key, Object value) {
        System.out.println("set ARP: index:" + index + ", value" + value);
        int index = Integer.parseInt(key);
        prlist[index].arp = value.toString();
    }

    public String getPrp(String key) {
        int index = Integer.parseInt(key);
        return prlist[index].prp;
    }

    public void setPrp(String key, Object value) {
        System.out.println("set PRP, Key:" + key + ", value:" + value);
        int index = Integer.parseInt(key);
        prlist[index].prp = value.toString();
    }
}

Earlier I was having this kind of working functions in struts 1 but now I am trying to move it to struts 2. Now my setter functions in HelloWorldAction.java for arp and prp are not getting called upon form submit.

public void setArp(String key, Object value);
public void setPrp(String key, Object value);  

<s:textfield name="prlist[0].arp" /> can work but we have some dependent code which requires to use fields with name such as <s:textfield name="arp(0)" />. I do not know whether struts 2 supports mapped properties or not. If it supports, then how can I use it.

I also found a related issue: https://issues.liferay.com/browse/LPS-14128

Note: I have made some modifications in question description

Thanks in advance.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36

1 Answers1

0

You're violating a lot of "rules" here, other than avoiding almost any mechanism provided by the framework. Never put logic in accessors / mutators (getters / setters), never use capitalized variable names, avoid using variables starting with an uppercase letter, read struts Type Conversion, use Struts tags (eg. <s:textfield/> instead of <input type="text" />) whenever possible, and reduce your code to:

public class HelloWorldAction{
    private String name;
    private List<PRLists> arnList = new ArrayList<PRLists>();

    public String execute() throws Exception {
       System.out.println("running execute");
       return "success";
    }

    public List<PRLists> getArnList(){
       return arnList;
    }
    public void setArnList(List<PRLists> arnList){
       this.arnList = arnList;
    }

    public String getName() {
       return name;
    }
    public void setName(String name) {
        System.out.println("set name: "+name);
        this.name = name;
    } 
}
<s:form action="hello" namespace="foo">    
    <s:textfield name="name" label="name" />
    <s:textfield name="arnList[0].arp" /> <br/>
    <s:textfield name="arnList[0].prp" /> <br/>
    <s:textfield name="arnList[1].arp" /> <br/>
    <s:textfield name="arnList[1].prp" /> <br/>
    <s:submit   value="Say Hello" /> 
</s:form>

of in an iterator, as you said (without showing it), like

<s:form action="hello" namespace="foo">
    <s:textfield name="name" label="name" />
    <s:iterator value="arnList" status="rowStatus">
        <s:textfield name="arnList[%{#rowStatus.index}].arp" /> <br/>
        <s:textfield name="arnList[%{#rowStatus.index}].prp" /> <br/>
    </s:iterator>
    <s:submit value="Say Hello" />
</s:form>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I do not want to use . Can I use the mapped properties the way I am using it. . As this was working for struts1. I have modified the question to correct some error, please reply back. – Harish Panwar Sep 01 '14 at 10:02
  • can work but we have some dependent code which requires to use fields with name such as . This is called mapped properties and I don't know whether this works in struts2 or not. – Harish Panwar Sep 01 '14 at 10:25
  • Are you talking [about this](http://struts.apache.org/release/2.3.x/docs/type-conversion.html#TypeConversion-Indexingacollectionbyapropertyofthatcollection) ? If yes, read [this answer](http://stackoverflow.com/a/24191304/1654265) too. But consider upgrading your *dependent code* to something standard, easy, and Struts2 compliant. Struts1 is another pair of shoes... the benefits of not rewriting a couple of tags would be easily surpassed by the damages of a bunch of complex hacks. – Andrea Ligios Sep 01 '14 at 12:02
  • I am looking for this: http://struts.apache.org/release/1.2.x/faqs/indexedprops.html#mappedprops – Harish Panwar Sep 01 '14 at 13:26
  • Tipycal of Struts1: messing with easy things just to make them complicated. BTW that is not a "feature", that is a hack, even on Struts1. If it is not working on S2, it probably means that you need another hack on top of that. You can probably hack the way parameters are sent to match your setter in a S2 compliant way, but you are bypassing OGNL and the framework mechanisms, and I still don't understand why would this be cheaper or cleaner than rewriting the tags. You still have to write code for this to work, even more. Is writing action code cheaper than writing JSP code ? – Andrea Ligios Sep 01 '14 at 14:12