yi_hcg
yi_hcg
采纳率0%
2017-08-14 10:18

struts2如何使用s:iterator标签把jsp页面的数据提交到action?

目前我的代码可以修改客户名字、客户年龄,但是无法修改客户订单。

客户类

 public class Customer {
    private Integer cid;
    private String cname;
    private Integer age;
    private Set<Orders> orders = new HashSet<Orders>();
    getter&setter
    public String toString() {
        return "Customer [cid=" + cid + ", cname=" + cname + ", age=" + age + ", orders=" + orders
                + "]";
    }

}

订单类

 public class Orders {
    private Integer oid;
    private String addr;
    private Customer customer;
    getter&setter
    public String toString() {
        return "Orders [oid=" + oid + ", addr=" + addr + "]";
    }

}

action

 public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

    private Customer customer = new Customer();
    public Customer getModel() {
        return customer;
    }

    private CustomerService customerService;
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public String upd() throws Exception{
        customerService.upd(customer);
        return SUCCESS;
    }

}

jsp

 <body>
<s:debug></s:debug>
<form action="${pageContext.request.contextPath }/customer_upd.action" method="post">
    <table border="1">
        <tr style="display: none">
            <td>客户id</td>
            <td><input type="text" name="cid" value="<s:property value="model.cid"/>"></td>
        </tr>
        <tr>
            <td>客户名字</td>
            <td><input type="text" name="cname" value="<s:property value="model.cname"/>"></td>
        </tr>
        <tr>
            <td>客户年龄</td>
            <td><input type="text" name="age" value="<s:property value="model.age"/>"></td>
        </tr>
        <tr>
            <td>客户订单</td>
            <td>
                <table border="1">
                    <tr>
                        <td>订单id</td>
                        <td>订单地址</td>
                        <td>订单客户id</td>
                    </tr>
                    <s:iterator value="model.orders" id="orders" status="st">
                        <tr>

                            <td><s:textfield name="orders.makeNew[%{#st.index}].oid" value="%{#orders.oid}" readonly="true" theme="simple"/></td>
                            <td><s:textfield name="orders.makeNew[%{#st.index}].addr" value="%{#orders.addr}" theme="simple"/></td>
                            <td><s:textfield name="orders.makeNew[%{#st.index}].customer.cid" value="%{#orders.customer.cid}" theme="simple" readonly="true"/></td>

                        </tr>
                    </s:iterator>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="修改"></td>
        </tr>
    </table>
</form>
</body>

参考http://bbs.csdn.net/topics/390648778?page=1
添加了CustomerAction-conversion.properties(不清楚写对了没)
KeyProperty_orders = oid
Element_orders = cn.xx.vo.Orders
CreateIfNull_orders =true
运行报错
WARN OgnlValueStack:64 - Error setting expression 'orders.makeNew[0].addrcidoid' with value '[Ljava.lang.String;@1d6d441'
ognl.OgnlException: Error getting property descriptor: null

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • yi_hcg yi_hcg 4年前

    几种尝试
    可以传值

    点赞 评论 复制链接分享
  • qq_35728177 Tsui丶 4年前

    http://www.cnblogs.com/chuliang/p/5906598.html 没碰到过你的情况,可以试下这个,另外你的客户id设置为display:none是无法传值的。(建议用hidden)

    点赞 评论 复制链接分享