springboot MVC
UF01-1844
Resources from spring.io
Example: Reserves management
Dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Model
public class Reservation {
private int idUser;
private int idPointCharge;
private Date dateStart;
private Date dateEnd;
private Boolean accepted;
}
Repository
@Repository
public class ReservationRepository {
}
Services
public interface ReservationService {
void reservationAdd(Reservation reservation);
List<Reservation> getAllReservations() ;
void reservationDelete(Reservation reservation);
void reservationDeleteById(int reservationId);
void reservationUpdate(int index,Reservation reservation);
reservationDetail(int index);
Reservation getReservationById(int index);
Reservation getReservationsByIdUser(int idUser);
Reservation
}
@Service
public class ReservationServiceImpl implements ReservationService{
@Autowired
private ReservationRepository reservationRepository;
// [.......]
}
Views
Annotacions used
- @Controller
- @GetMapping
- @PostMapping
- @RequestMapping
using dates
One approach is set date format in application.properties
.mvc.format.date=yyyy-MM-dd
spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
spring.mvc.format.time=HH:mm:ss spring
Be careful with the mapping. html input type and property of attribute java class.
Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors<EOL>Field error in object 'reservation' on field 'dateEnd': rejected value [13/12/2023];
codes [typeMismatch.reservation.dateEnd,typeMismatch.dateEnd,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservation.dateEnd,dateEnd]; arguments [];
default message [dateEnd]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateEnd'; Failed to convert from type [java.lang.String] to type [java.util.Date] for value '13/12/2023']
Field error in object 'reservation' on field 'dateStart': rejected value [12/12/2023];
codes [typeMismatch.reservation.dateStart,typeMismatch.dateStart,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservation.dateStart,dateStart]; arguments []; default message [dateStart]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateStart';
Failed to convert from type [java.lang.String] to type [java.util.Date] for value '12/12/2023']]
<input type="text" class="form-control" th:field="*{dateStart}" />
The correct html type is date, type=“date”
content of example on github
lab delivery
Code (UF01-1844)