Spring MVC
Spring MVC
petición HTTP
navegador Controlador Modelo
re
sp
ue dato
st
a
HT dato
TP
Vista
Base de
datos
Spring MVC
1
https:
//docs.spring.io/spring-framework/reference/web/webmvc.html
2
https://spring.io/projects/spring-framework
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 8
Spring Boot
3
https://spring.io/projects/spring-boot
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 9
El componente del modelo
4
ORM: mapeo objeto relacional.
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 10
El componente del modelo
5
https://spring.io/projects/spring-data-jpa
6
https://jakarta.ee/specifications/persistence/
7
https://hibernate.org/orm/
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 12
El componente del modelo con Spring Data JPA
/*
* Declaraci ó n de una clase del modelo
*/
@Entity
public class User {
@Id
@ Ge ne ra t ed Va lu e ( strategy = Gener ationTyp e . AUTO )
private Integer id ;
@Column ( nullable = false )
@NotBlank
@Size ( max = 64)
private String name ;
@Column ( unique = true , nullable = false )
@Email
@NotBlank
@Size ( max = 128)
private String email ;
public Integer getId () {
return id ;
}
public void setId ( Integer id ) {
this . id = id ;
}
(...)
}
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 13
El componente del modelo con Spring Data JPA
/*
* Declaraci ó n de una clase repositorio
*/
public interface U serRepos itory
extends CrudRepository < User , Integer > {
User findByEmail ( String email ) ;
}
// Inserci ó n de un objeto
User user = new User () ;
user . setName ( " Mary " ) ;
user . setEmail ( " mary@example . com " ) ;
user Reposito ry . save ( user ) ;
8
https://www.thymeleaf.org/
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 16
Thymeleaf
@Controller
@ Re qu es t Ma pp i ng ( path = " / " )
public class M e s s a g e C o n t r o l l e r {
@Autowired
private M e s s a g e R e p o s i t o r y m e s s a g e R e p o s i t o r y ;
@GetMapping ( path = " / message /{ messageId } " )
public String viewMessage (
@PathVariable ( " messageId " ) int messageId ,
Model model ) {
Message message = m e s s a g e R e p o s i t o r y . findById ( messageId ) ;
if ( message != null ) {
model . addAttribute ( " message " , message ) ;
return " view_message " ;
} else {
throw new R e s p o n s e S t a t u s E x c e p t i o n (
HttpStatus . NOT_FOUND , " Message ␣ not ␣ found " ) ;
}
}
(...)
}
< form action = " find - user " method = " get " >
< label >
Search user :
< input type = " email " name = " email "
placeholder = " email ␣ address " >
</ label >
< input type = " submit " value = " Search " >
</ form >
9
Los beans son clases Java que encapsulan datos (propiedades) y siguen el
convenio de proporcionar un constructor sin argumentos y métodos de acceso
(getters y setters) a las propiedades.
Aplicaciones Web (2023/24) Desarrollo de aplicaciones Web con Spring MVC 26
Parte IV
Plantillas Thymeleaf
<p >
Your name is < span th : text = " $ { user . name } " > John </ span >
and you are from
< span th : text = " $ { user . country } " > Nowhere </ span >.
</ p >
< ul >
< li th : each = " user ␣ : ␣ $ { users } " th : text = " $ { user . name } " >
John Doe
</ li >
</ ul >
< div class = " message " th : each = " message ␣ : ␣ $ { messages } " >
< div class = " text " th : text = " $ { message . getText () } " >
Text of the post
</ div >
< div class = " metadata " >
< span class = " author "
th : text = " $ { message . getUser () . getName () } " >
Author ' s name
</ span >
</ div >
</ div >
<p >
< span th : text = " $ { user . getName () } " > John Doe </ span >
< span th : if = " $ { user . isAdmin () } " >
( admin )
</ span >
</ p >
Rutas relativas
https://www.example.com/app/main.html
<html>
<head>
(...)
</head>
<body>
<p>
<img src="foto.jpg"
alt="Una foto"> https://www.example.com/app/foto.jpg
</p>
</body>
</html>