从execute()方法中的列表中检索值
我想要显示的项目列表以及每个都有一个空白文本字段来填充我的JSP页面上的金额,一旦我填写了所需的项目金额,我将提交。
我有如下的ExampleAction
类,其中我有populate()方法,我首先触发,以便填充项目。 我点火了:
http://localhost:8084/WebExample/populate.action.
相同的ExampleAction
执行mtd,我从JSP页面调用SUBMIT按钮操作。 但我的问题是在执行方法,我无法获取列表中的对象,即exList。 这是因为动作类的实例只与一个请求相关联吗? 当我通过SUBMIT按钮触发另一个动作时,有不同的值堆栈关联? 如果是,那么我应该以最好的方式检索输入的那些金额(在JSP中),在execute()方法中在Tomcat的控制台中打印?
ExampleAction:
private ArrayList exList; private EX ex; public ExampleAction(){ exList = new ArrayList(); } //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:
struts.xml中:
/register/example.jsp /register/example.jsp
要将值提交到ArrayList
您可以使用索引属性名称。 例如
您还应该为Bean Ex
提供转换配置。 假设属性name
在bean内部。
@Element(value = Ex.class) @Key(value = String.class) @KeyProperty(value = "name") @CreateIfNull(value = true) private ArrayList exList;
请注意,您可以省略一些注释,因为可能是未使用的键或默认使用该值,但它在所有情况下都是可配置的。 有关此转换技术的更多信息,请参阅文档高级类型转换 。
编辑:
还有一件事EX!= Ex,它应该是公共类而不是内部类。 必须设置浮点值,检查您是否没有转换错误。
从中移除action="ex"
因为在提交时单击表单动作调用。 无需在提交按钮中给出action="ex"
。
关闭标签。