1

enter image description here

I want list of items to be displayed along with each having a blank textfield to fill amount on my JSP page and once I fill the required items amount, I will submit.

I have ExampleAction class as below, where I have populate() method which I fire first so that items are filled. I fire URL :

http://localhost:8084/WebExample/populate.action.

Same ExampleAction has execute mtd which I call on SUBMIT button action from JSP page. But my problem is in execute method, I am unable to get the objects in list i.e. exList. Is this because an instance of the action class is associated with just one request? And when I fire another action through SUBMIT button there is different value stack associated? If yes, what should be the best possible ways for me to retrieve those amounts entered (in JSP), in execute() method to print in console of Tomcat?

ExampleAction:

private ArrayList<EX> exList;
private EX ex;

  public ExampleAction(){
      exList = new ArrayList<EX>();
}

//Getters And Setters.
@Override
public String execute() throws Exception {
     for (EX ex1 : exList) {
        System.out.println("ex1 = " + ex1.getName());
    }

    return SUCCESS;
}

public String populate() throws Exception{
    System.out.println("in populate");
    exList.add(new EX("Item 1",0.0f));
    exList.add(new EX("Item 2",0.0f));
    ![enter image description here][2]...
    ...  
    return SUCCESS; 
}

EX.class:

class EX {

private String name;
private float amt;

public EX(String name, float amt) {
    this.name = name;
    this.amt = amt;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public float getAmt() {
    return amt;
}

public void setAmt(float amt) {
    this.amt = amt;
}}

example.jsp:

<form action="ex">
    <div style="overflow: auto;height: 200px;width: 400px; border: black solid;border-style: double;">
    <table border="1">
        <tr>
            <td>Name</td>
            <td>Amt</td> 
        </tr>
    <s:iterator value="exList" var="ex">
        <tr>
            <td><s:property value="name"/></td>
            <td><s:textfield cssClass="num" onchange="calculateSum()"/></td>
        </tr>
    </s:iterator>
    </table>
    </div>
    <br>
    Sum : <s:textfield  cssClass="ssum" disabled="true"/> 
    <br>
    <s:submit  action="ex" value="SUBMIT"/>
    </form>

Struts.xml:

<package name="default" extends="struts-default" namespace="/">     
    <action name="populate" class="com.ex.register.ExampleAction" method="populate">
        <result name="success">/register/example.jsp</result>
    </action>

    <action name="ex" class="com.ex.register.ExampleAction" method="execute">
        <result name="success">/register/example.jsp</result>
    </action> </package></struts>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Rahul Y
  • 38
  • 7
  • You can persist the values on submit and retrieve back to show them on the next request. – Roman C May 23 '14 at 11:26
  • @RomanC I want to retrieve data in execute(), when I click submit and request is forwarded to execute() mtd in ExampleAction. Can you pls suggest me the ways I can persist amounts entered in those textfields? – Rahul Y May 23 '14 at 12:00

2 Answers2

1

To submit the values to ArrayList you can use indexed property names. For example

<s:iterator value="exList" var="ex" status="status">
    <tr>
        <td><s:property value="name"/></td>
        <td><s:textfield cssClass="num" name="exList[%{status.index}].name" onchange="calculateSum()"/></td>
    </tr>
</s:iterator>

You should also provide a conversion configuration for the bean Ex. Assume the property name is inside the bean.

@Element(value = Ex.class)
@Key(value = String.class)
@KeyProperty(value = "name") 
@CreateIfNull(value = true)
private ArrayList<EX> exList;

Note that some annotations you can omit, because might be the key is not used or the value is used by default, but it's configurable in all cases. More about this conversion technique you can find in the docs Advanced Type Conversion.

EDIT:

One more thing EX != Ex and it should be public class not an inner class. Having to set float values check that you don't have conversion errors.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I did the changes as you suggested but still I am unable to get the objects in exList i.e. it is showing its size = 0 in execute() mtd. Pls can any one tell me why I am getting size=0 and soln for it. – Rahul Y May 24 '14 at 12:03
  • @RahulY Are you using `ModelDriven`? Do you have getters and setters for properties? – Roman C May 24 '14 at 12:09
  • No, I am not using ModelDriven and I have getters and setters for properties as in ExampleAction above. I think, First I fired populate.action which populated and displayed the exList but when I fire another action (ex.action) from SUBMIT button and access execute() mtd, exList object is different. Am I right? – Rahul Y May 24 '14 at 13:00
  • Could you post EX class? It should have a default constructor, so Struts is able to instantiate it and set the property. – Roman C May 24 '14 at 14:07
  • You should create a default constructor `public Ex(){}`. – Roman C May 24 '14 at 18:43
  • I created default constructor but still I am getting exList size = 0 – Rahul Y May 25 '14 at 12:53
  • I am really thankful to you, My issue is solved. Thanks for drawing my attention to inner class. I made it public and my issue is gone. Of course in future I will not break any known rules of SO. If you suggest I can delete that question. Also if you could enter this comment as answer I can mark it as 'Accepted Answer' so that others are benefited too. – Rahul Y May 29 '14 at 11:43
  • @RahulY I edited the answer, you can accept and upvote the answer, and delete your duplicate answer. Because it unlikely to have duplicate questions on SO, misleading future readers. – Roman C May 29 '14 at 12:23
  • please can you guide me, to delete my duplicate question. – Rahul Y May 29 '14 at 12:34
  • Can you press delete link? – Roman C May 29 '14 at 12:48
  • Oh I am sorry I missed that link. So now I have deleted it. Thank You once again. – Rahul Y May 29 '14 at 12:55
  • Welcome to SO, it's a great site. – Roman C May 29 '14 at 12:59
0

Remove action="ex" from

<s:submit  action="ex" value="SUBMIT"/>

because on submit click form action calls. no need to give action="ex" in submit button.

Close </package> tag.

Paresh3489227
  • 845
  • 11
  • 22