Spring boot Thymeleaf problème de controleur
Rune188
Messages postés
81
Statut
Membre
-
Rune188 Messages postés 81 Statut Membre -
Rune188 Messages postés 81 Statut Membre -
Bonjour,
j'essaye de faire une application web qui utilise:
SpringBoot
Mysql
JDBC
Design pattern: MVC, DAO
Thymeleaf
Et IntelliJ
Mais je rencontre un problème avec la redirection des contrôleurs vers les vues:

index.html:
LoginController (le controleur):
loginPage.html (une des vues qui renvoi le message d'erreur précédent):

j'essaye de faire une application web qui utilise:
SpringBoot
Mysql
JDBC
Design pattern: MVC, DAO
Thymeleaf
Et IntelliJ
Mais je rencontre un problème avec la redirection des contrôleurs vers les vues:
index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to Spring Boot</h1>
<a th:href="@{/login}">Login</a>
</body>
</html>
LoginController (le controleur):
package com.example.MyTFE.controllers;
import com.example.MyTFE.model.DAO.DiabeticDAO;
import com.example.MyTFE.model.bean.LoginForm;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class LoginController {
//to get login form page
@RequestMapping(value="login", method= RequestMethod.GET)
public String getLoginForm(){
//return html page name
return "loginPage";
}
//checking for login credentials
@RequestMapping(value="login", method= RequestMethod.POST)
public String login(@ModelAttribute (name="loginForm")LoginForm loginForm, Model model) {
String mail= loginForm.getMail();
String mdp = loginForm.getMdp();
DiabeticDAO dia = new DiabeticDAO();
if(dia.loggin(mail,mdp))
{
return "home";
}
else
{
return "firstPage";
}
}
}
loginPage.html (une des vues qui renvoi le message d'erreur précédent):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Title</title>
</head>
<body>
<form action="#" th:action="@{/login}" th:object="${loginForm}" method="post">
<div class="mb-3 mt-3">
<label for="email" class="form-label">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email" autofocus="autofocus">
</div>
<div class="mb-3">
<label for="mdp" class="form-label">Password:</label>
<input type="password" class="form-control" id="mdp" placeholder="Enter password" name="mdp">
</div>
<button type="submit" value = "Log In" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
A voir également:
- Cannot invoke "org.springframework.jdbc.core.jdbctemplate.query(string, org.springframework.jdbc.core.rowmapper)" because "this.jdbctemplate" is null
- Dual boot - Guide
- Hiren's boot - Télécharger - Divers Utilitaires
- Boot camp - Télécharger - Systèmes d'exploitation
- Clé boot windows - Guide
- Hiren's BootCD - Télécharger - Divers Utilitaires
1 réponse
Une partie du problème semble venir du fait que j'ai oublié l'annotation "@Controller" mais j'obtient toujours le même message d'erreur (mais moins) en plus de celui-ci qui m'indique que "this.jdbcTemplate" est null et je n'arrive pas à trouver pourquoi
"java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)" because "this.jdbcTemplate" is null
at com.example.MyTFE.model.DAO.DiabeticDAO.getAllDiab(DiabeticDAO.java:33) ~[classes/:na]
at com.example.MyTFE.model.DAO.DiabeticDAO.loggin(DiabeticDAO.java:39) ~[classes/:na]
at com.example.MyTFE.controllers.LoginController.login(LoginController.java:27) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.19.jar:5.3.19]
...
"java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)" because "this.jdbcTemplate" is null
at com.example.MyTFE.model.DAO.DiabeticDAO.getAllDiab(DiabeticDAO.java:33) ~[classes/:na]
at com.example.MyTFE.model.DAO.DiabeticDAO.loggin(DiabeticDAO.java:39) ~[classes/:na]
at com.example.MyTFE.controllers.LoginController.login(LoginController.java:27) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.19.jar:5.3.19]
...