我深化發展使用Java EE,EJB & JPA(EclipseLink的)航空公司管理Web應用程序(JAVA EE)方法不工作不拋出任何錯誤
我有,當我打電話的方法的麻煩在servlet的服務層中:該方法沒有做任何預期的事情而沒有任何錯誤。
該方法是AddPassengerToFlight,在那裏我試圖添加一個乘客到乘客列表,我在具有ManyToMany關係的飛行實體中。
這是priject的結構。
這是飛行和乘客實體whitout getter和setter:
飛行:
public class Flight implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Enumerated(EnumType.STRING)
private Cities depart;
@Enumerated(EnumType.STRING)
private Cities destination;
private String prix;
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinTable(name = "f_p_join", joinColumns = @JoinColumn(name = "flight_fk"), inverseJoinColumns = @JoinColumn(name = "passenger_fk"))
private List<Passenger> passengers;
@OneToOne()
@JoinColumn(name = "airplane_fk")
private Plane airplane;
@OneToOne()
@JoinColumn(name = "pilot_fk")
private Pilot pilot;
}
乘客:
public class Passenger implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String nom;
private String prenom;
private String adresse;
private String telephone;
private String email;
@Enumerated(EnumType.STRING)
private FlightClass flightClass;
@ManyToMany(mappedBy = "passengers")
private List<Flight> flights;
}
的PassenerService其中i有方法:
public class PassengerService {
private static final String CHAMP_FLIGHT_ID = "flightId";
private static final String CHAMP_PASSENGER_ID = "passengerId";
private String resultat;
private Map<String, String> erreurs = new HashMap<String, String>();
private PassengerDao passengerDao;
private FlightDao flightDao;
private PilotDao pilotDao;
private PlaneDao planeDao;
public Map<String, String> getErreurs() {
return erreurs;
}
public String getResultat() {
return resultat;
}
public PassengerService(PassengerDao passengerDao, FlightDao flightDao, PilotDao pilotDao, PlaneDao planeDao) {
this.passengerDao = passengerDao;
this.flightDao = flightDao;
this.pilotDao = pilotDao;
this.planeDao = planeDao;
}
public Passenger createPassenger(HttpServletRequest request) {
//The createPassenger code
}
public List<Passenger> getAllPassengers() {
//The getAllPassengers Code
}
public void AddPassengerToFlight(HttpServletRequest request) {
FlightService flightService = new FlightService(flightDao, planeDao, pilotDao);
String passengerId = request.getParameter(CHAMP_PASSENGER_ID);
String flightId = request.getParameter(CHAMP_FLIGHT_ID);
Passenger passenger = getPassengerById(passengerId);
Flight flight = flightService.getFlightById(flightId);
List<Passenger> pList = flight.getPassengers();
pList.add(passenger);
flight.setPassengers(pList);
passenger.getFlights().add(flight);
}
public Passenger getPassengerById(String passengerId) {
Passenger passenger = passengerDao.getPassengerById(passengerId);
return passenger;
}
public void removePassenger(Passenger passenger) {
//The removePassenger code
}
}
這是servlet AddPassengerToFlight
@WebServlet("/AddPassengerToFlight")
public class AddPassengerToFlight extends HttpServlet {
private static final String VUE_REDIRECT = "/airline/PassengerList";
private static final long serialVersionUID = 1L;
@EJB
FlightDao flightDao;
@EJB
PilotDao pilotDao;
@EJB
PlaneDao planeDao;
@EJB
PassengerDao passengerDao;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
FlightService flightService = new FlightService(flightDao, planeDao, pilotDao);
PassengerService passengerService = new PassengerService(passengerDao, flightDao, pilotDao, planeDao);
List<Flight> flights = flightService.getAllFlights();
List<Passenger> passengers = passengerService.getAllPassengers();
request.setAttribute("flights", flights);
request.setAttribute("passengers", passengers);
this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
PassengerService passengerService = new PassengerService(passengerDao, flightDao, pilotDao, planeDao);
passengerService.AddPassengerToFlight(request);
response.sendRedirect(VUE_REDIRECT);
}
}
這是形式AddPassengerToFlight(JSP文件)
<div class="container">
<h2>Adding Passenger To Flight</h2>
<br/><br/>
<form method="POST" action="AddPassengerToFlight">
<div class="form-group">
<h4>Flight :</h4>
<select name="flightId" class="selectpicker" data-live-search="true" data-width="100%">
<c:forEach items="${flights}" var="flight">
<option value="${flight.id}">
FROM <c:out value="${flight.depart}" /> TO <c:out value="${flight.destination}" /> AT <c:out value="${flight.date}" />
</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<h4>Passenger :</h4>
<select name="passengerId" class="selectpicker" data-live-search="true" data-width="100%">
<c:forEach items="${passengers}" var="passenger">
<option value="${passenger.id}"><c:out value="${passenger.nom}" /></option>
</c:forEach>
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
當我點擊重定向到PassengerList的AddPassengerToFlight IM的按鈕提交,但什麼也沒有發生兩個實體(飛行和乘客)之間,我沒有錯誤。
任何人都可以弄清楚我在這裏做錯了嗎?
嗨@Carlitos&謝謝你的幫助。事實上,我有一個服務層與無狀態的EJB和方法工作我有另一個問題,當我做表格驗證我不能通過結果和錯誤映射到servlet通過注入一個無狀態的EJB,所以我改變服務到普通的JAVA類,但我認爲你是對的,我必須有一個EJB,所以我再次將其更改爲有狀態EJB,以便我可以將表單驗證的結果和錯誤地圖傳遞給該servlet。現在表單驗證正在工作,但AddPassengerToFlight方法仍然不起作用。 –
在servlet中,我通過注入DAO並將它們傳遞給servlet的構造函數來實例化服務(就像我對普通JAVA對象所做的那樣),因爲我無法在servlet中注入有狀態的EJB。 –
爲什麼你需要一個有狀態的EJB? –