小编典典

发送迭代器创建的表单的多行以进行操作

jsp

我对杆很陌生。我正在创建一个示例Web应用程序。下面是一个JSP页面,该页面从用户那里获取值。“乘客”指的是我要创建的行数,该行在上一操作的值堆栈中可用。

<s:form method="post" action="passengerDetailsEntered">
    <tbody>
        <s:iterator begin="0" end="passengers-1">
          <tr>
            <td><s:property value="top+1"/></td>
            <td><s:textfield name="passengerDetails.name"></s:textfield></td>
            <td><s:textfield name="passengerDetails.age"></s:textfield></td>
            <td><s:select list="#{'M':'Male','F':'Female'}" 
                          headerValue="Select" headerKey="-1" 
                          name="passengerDetails.sex"></s:select>
            </td>
          </tr>
        </s:iterator>
        <s:hidden name="totalFare" value="%{totalFare}"/>
        <s:submit value="Submit" id="select"></s:submit>
    </tbody>
</s:form>

因此,如果我想添加3名乘客的详细信息,那么乘客的价值将为3。现在,当我提交此表单时,我的期望是,迭代器本身将创建一个包含两个类型为PassengerDetails的对象的列表(如果passenger的值为2)。以下是我的PassengerDetails
POJO。

      package com.model;
        import java.io.Serializable;
        import javax.persistence.Column;
        import javax.persistence.Entity;
        import javax.persistence.FetchType;
        import javax.persistence.Id;
        import javax.persistence.JoinColumn;
        import javax.persistence.ManyToOne;
        import javax.persistence.Table;

      @Entity
@Table(name="passengerdetails")
public class PassengerDetails implements Serializable{

    private static final long serialVersionUID = 4786387763215540175L;
    private String name;
    private int age;
    private char sex;
    private TicketDetails ticketDetails;
    @Id
    private int passengerid;

    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="age")
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Column(name="sex")
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }

    @ManyToOne(fetch = FetchType.EAGER)  
    @JoinColumn(name="tid")  
    public TicketDetails getTicketDetails() {
        return this.ticketDetails;
    }
    public void setTicketDetails(TicketDetails ticketDetails) {
        this.ticketDetails = ticketDetails;
    }
}

以下是我的动作类TicketBookingAction,并且在执行动作“
passengerDetailsEntered”时调用了该方法的passengerDetails:

    package com.view;

import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.controller.FetchDetailsManager;
import com.controller.PassengerDetailsManager;
import com.controller.TicketDetailsManager;
import com.model.PassengerDetails;
import com.model.ScheduleDetails;
import com.model.TicketDetails;
import com.model.UserDetails;
import com.opensymphony.xwork2.ActionContext;

public class TicketBookingAction{


    private List<PassengerDetails> passengerDetails;
    public List<PassengerDetails> getPassengerDetails() {
        return passengerDetails;
    }
    public void setPassengerDetails(List<PassengerDetails> passengerDetails) {  
        this.passengerDetails=passengerDetails;
    }

    public String passengerDetails()
    {
        System.out.println("Name is");
        System.out.println(getPassengerDetails().get(0).getName());
        System.out.println(getPassengerDetails().get(0).getAge());
        System.out.println(getPassengerDetails().get(0).getSex());
        System.out.println(getPassengerDetails().get(1).getName());
        System.out.println(getPassengerDetails().get(1).getAge());
        System.out.println(getPassengerDetails().get(1).getSex());
        return "passengerDetailsFilled";
    }
}

但是它将名称打印为空。根据用户输入的年龄打印。性别被印成某种垃圾人物。有人可以帮助我提出背后的概念吗?我应该怎么做才能在动作类的表单上获取我输入的所有值。提前致谢。


阅读 238

收藏
2020-06-10

共1个答案

小编典典

指定一个索引,以引用每次迭代的右行:

<s:iterator begin="0" end="passengers-1" status="stat">
  <tr>
    <td><s:property value="top+1"/></td>
    <td><s:textfield name="passengerDetails[%{#stat.index}].name" /></td>
    <td><s:textfield name="passengerDetails[%{#stat.index}].age" /></td>
    <td><s:select list="#{'M':'Male','F':'Female'}" 
                  headerValue="Select" headerKey="-1" 
                  name="passengerDetails[%{#stat.index}].sex" />
    </td>
  </tr>
</s:iterator>

提示一下,如果里面没有东西,请自行关闭标签。它将更具可读性。

2020-06-10