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>
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.
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.
来源:https://stackoverflow.com/questions/23827729/retrieving-values-from-list-in-execute-method