Hi,
A few weeks ago I posted my problem I could not persist my records.
I still have this problem but I fixed some errors.
I have a Form with a few textboxes to make a reservation for a hotel.
It's need your name,firstame, arrival etc...
When I press on my save button it doesnt persist and i dont know why
I put some system.outprintlns to check my function does receive the parameters and it does ....
I worked on this way:
My Reservationcontroller
My ReservationDaoCode:package eu.test.test4.controller; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import be.hub.web4.dao.ReservationDAO; import be.hub.web4.model.Reservation; @Controller public class ReservationController { @Autowired private ReservationDAO reservationdao; @RequestMapping(value={"/newReservation"},method=RequestMethod.GET) public String ReservationFormulier(ModelMap model){ Reservation reservation = new Reservation(); model.addAttribute("dereservatie", reservation); return "/newReservation"; } @RequestMapping(value={"/newReservation"},method=RequestMethod.POST) public String ReservationMaken(@ModelAttribute("dereservatie") @Valid Reservation reservation, BindingResult result, ModelMap model){ System.out.println("Controller wordt opgeroepen"); reservationdao.saveReservation(reservation.getRoomId(),reservation.getFirstname(), reservation.getLastname(),reservation.getArrival(),reservation.getDeparture(),reservation.getNumberOfNights(),reservation.getNumberOfPersons(),reservation.getPrice()); System.out.println("Controllertest second time : " + reservation.getFirstname() ); return "home"; } }
My controllerImplementationCode:package eu.test.test4.dao; import eu.test.test4.Reservation; public interface ReservationDAO { public Reservation saveReservation(int roomId, String firstname, String lastname, String arrival, String departure,String numberOfNights,String numberOfPersons,double price); }
My domainCode:package eu.test.test4.dao; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import be.hub.web4.model.Reservation; @Repository @Transactional public class ReservationDAOcoll implements ReservationDAO{ @PersistenceContext private EntityManager em; //@Transactional public Reservation saveReservation(int roomId, String firstname, String lastname, String arrival, String departure, String numberOfNights, String numberOfPersons, double price) { Reservation reservation = new Reservation(); reservation.setRoomId(roomId); reservation.setFirstname(firstname); System.out.println("The firstname: " + reservation.getFirstname()); reservation.setLastname(lastname); reservation.setArrival(arrival); reservation.setDeparture(departure); reservation.setNumberOfNights(numberOfNights); reservation.setNumberOfPersons(numberOfPersons); reservation.setPrice(price); System.out.println("ReservationDaoImpl saveReservation is called" + firstname + " " + lastname + " successfully booked:" + arrival + " till: " + departure + " #nights: " + numberOfNights + " #persons:" + numberOfPersons + " Price: " + price); //em.persist(reservation); em.merge(reservation); return reservation; } }
My viewCode:package eu.test.test4.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; //import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; //import javax.validation.constraints.Digits; import org.hibernate.validator.constraints.NotEmpty; //import org.hibernate.validator.constraints.*; @Entity @Table(name = "tblreservations") public class Reservation { @Id@GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "reservationId") private int reservationId; @Column(name = "roomId") private int roomId; //@Length(min = 2) @Column(name = "firstname", nullable = false) @NotEmpty(message="Vul naam in aub") private String firstname; //@Length(min = 4) @Column(name = "lastname") private String lastname; @Column(name = "arrival") //@NotEmpty private String arrival; @Column(name = "departure") //@NotEmpty private String departure; //@Digits(integer = 3, fraction = 0) @Column(name = "numNights") private String numberOfNights; //@Digits(integer = 3, fraction = 0) @Column(name = "numPers") private String numberOfPersons; @Column(name = "price") private double price; public Reservation(){ } public Reservation(int roomId, String firstname, String lastname, String arrival,String departure, String numberOfNights, String numberOfPersons, double price){ this.roomId = roomId; this.firstname = firstname; this.lastname = lastname; this.arrival = arrival; this.departure = departure; this.numberOfNights = numberOfNights; this.numberOfPersons = numberOfPersons; this.price = price; } public int getReservationId() { return reservationId; } public void setReservationId(int reservationId) { this.reservationId = reservationId; } public int getRoomId() { return roomId; } public void setRoomId(int roomId) { this.roomId = roomId; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getArrival() { return arrival; } public void setArrival(String arrival) { this.arrival = arrival; } public String getDeparture() { return departure; } public void setDeparture(String departure) { this.departure = departure; } public String getNumberOfNights() { System.out.println("Get number of nights is called" + " " + numberOfNights); return numberOfNights; } public void setNumberOfNights(String numberOfNights) { System.out.println("Set number of nights is called"); this.numberOfNights = numberOfNights; } public String getNumberOfPersons() { return numberOfPersons; } public void setNumberOfPersons(String numberOfPersons) { this.numberOfPersons = numberOfPersons; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Thnx!Code:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <p>New Reservation</p> <c:url var="url" value="/newReservation.html" /> <form:form action="${url}" commandName="dereservatie"> <%-- Spring form tags --%> <fieldset> <div> <label>roomId:</label><form:input path="roomId"/> </div> <div> <label>firstname:</label><form:input path="firstname"/> </div> <div> <label>lastname:</label><form:input path="lastname"/> </div> <div> <label>arrival:</label><form:input path="arrival"/> </div> <div> <label>departure:</label><form:input path="departure"/> </div> <div> <label>numberOfNights:</label><form:input path="numberOfNights"/> </div> <div> <label>numberOfPersons:</label><form:input path="numberOfPersons"/> </div> <div> <label>price:</label><form:input path="price"/> </div> <div><input name="submit" type="submit" value="save"/></div> </fieldset> </form:form> </body> </html>



Reply With Quote