Web in springBoot

This commit is contained in:
ExostFlash 2025-04-07 14:06:26 +02:00
parent 5fb4530b79
commit 574cc6dbb2
4 changed files with 76 additions and 0 deletions

1
.idea/gradle.xml generated
View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View file

@ -0,0 +1,48 @@
package fr.teamflash.messageries.controllers
import fr.teamflash.messageries.models.UserBean
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.servlet.mvc.support.RedirectAttributes
@Controller
class UserController {
@GetMapping("/login")
fun login(model: Model, userBean: UserBean): String {
model.addAttribute("login", userBean.login)
model.addAttribute("password", userBean.password)
return "login"
}
@PostMapping("/loginSubmit") //traite le formulaire
fun loginSubmit(userBean: UserBean, redirect: RedirectAttributes) : String {
try {
if (userBean.login.isBlank()) {
throw Exception("Login manquant")
}
if (userBean.password.isBlank()) {
throw Exception("Mot de passe manquant")
}
//Cas qui marche
redirect.addFlashAttribute("userBean", userBean)
return "redirect:userRegister" // Redirection sur /userRegister
} catch (e:Exception) {
e.printStackTrace()
//Cas d'erreur
redirect.addFlashAttribute("errorMessage", e.message)
redirect.addFlashAttribute("userBean", userBean)
return "redirect:login" //Redirige sur /login
}
}
@GetMapping("/userRegister") //Affiche la page résultat
fun userRegister(model: Model, userBean: UserBean): String {
model.addAttribute("userBean", userBean)
return "userRegister" //Lance userRegister.html
}
}

View file

@ -0,0 +1,8 @@
<!-- th:action="'/loginSubmit'" ou action="/loginSubmit" -->
<form method="post" action="/loginSubmit" th:object="${userBean}">
<p>Login: <input th:field="*{login}" type="text"/></p>
<p>Password: <input th:field="*{password}" type="password"/></p>
<p><input type="submit" value="Submit"/></p>
</form>

View file

@ -0,0 +1,19 @@
<!doctype html>
<html lang="fr-FR">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="/css/styles.css" rel="stylesheet">
</head>
<body>
<h1 th:text="'Bonjour ' + ${userBean.login}"></h1>
</body>
</html>