从execute()方法中的列表中检索值 [英] Retrieving values from list in execute() method

查看:138
本文介绍了从execute()方法中的列表中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要要显示的项目列表,每个项目都有一个空白文本字段,以填充我的JSP页面上的金额,一旦我填写了所需的项目金额,我将提交。

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.

我有 ExampleAction 类,如下所示,我有populate()方法,我首先触发,以便填充项目。我解雇URL:

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.

相同 ExampleAction 执行mtd我打电话来自JSP页面的SUBMIT按钮操作。但我的问题是在执行方法,我无法获取列表中的对象,即exList。
这是因为动作类的实例只与一个请求相关联吗?当我通过SUBMIT按钮触发另一个动作时,有不同的值堆栈关联?如果是,那么我应该以最好的方式检索输入的那些金额(在JSP中),在execute()方法中在Tomcat的控制台中打印?

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>


推荐答案

将值提交到 ArrayList 您可以使用索引属性名称。例如

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>

您还应该为bean提供转换配置 Ex 。假设属性 name 在bean中。

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.

编辑:

还有一件事EX!= Ex它应该是公共课不是内在阶级。必须设置浮点值,检查您是否没有转换错误。

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.

这篇关于从execute()方法中的列表中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆