我对struts很陌生。我正在创建一个示例 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。它在这里工作正常。现在我的预期是,当我提交此表单时,如果乘客的值为 2,迭代器本身会创建一个包含两个 PassengerDetails 类型的对象的列表。“passengerDetails”是PassengerDetails 类型的对象。以下是我的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 和它的方法 PassengerDetails 在执行动作“passengerDetailsEntered”时被调用:
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";
}
}
但它将名称打印为空。年龄按用户输入的方式打印。性被打印为一些垃圾字符。任何人都可以帮我解决这背后的概念吗?我应该怎么做才能让我在我的动作课的表格上输入的所有值。提前致谢。