Bonjour,


J'ai un problème sur l'application que je développe que je n'arrive pas du tout à résoudre, je ne comprends pas pourquoi cela ne fonctionne pas. Je récupère des informations entrées par l'utilisateurs sur une ihm en l'occurrence ici un nom et prenom et a l'aide d''hibernate je vais chercher en base les infos sur la personne entrées. Toutefois je n'arrive pas a récupérer via mon formulaire les données saisies, cela me renvoie null. Je précise que je suis un débutant en spring :/

Voici les classes que j'utilises :

-Controller :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
package edu.sodteam.controller;
 
 
import edu.sodteam.bean.Advisor;
import edu.sodteam.form.SearchAdvisorForm;
import edu.sodteam.service.IServiceListAdvisors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.context.MessageSource;
import javax.validation.Valid;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
 
 
@Controller
public class SearchAdvisorsController {
 
    private static final Logger LOGGER = Logger.getLogger( SearchAdvisorsController.class.getName() );
 
    @Autowired
    private MessageSource messageSource;
 
 
    @Autowired
    IServiceListAdvisors service;
 
    @GetMapping("/searchadvisor")
    public String showSearchForm(ModelMap model) {
        model.addAttribute("searchAdvisorForm", new SearchAdvisorForm());
//        LOGGER.log(Level.INFO, "Displays the form to search the Advisors\n");
//        if(model.get("searchAdvisorForm")==null){
//            model.addAttribute("searchAdvisorForm", new SearchAdvisorForm());
//            LOGGER.log(Level.INFO, "Displays the form to search the Advisors if it doesn't exist\n");
//        }
        return "advisor.search";
    }
 
    @PostMapping("/searchOneAdvisor")
    public String showResultSearch(@Valid @ModelAttribute(value="searchAdvisorForm") SearchAdvisorForm advisorForm
                                                                                    ,BindingResult pbinding, ModelMap model){
        if(!pbinding.hasErrors()) {
            String name = advisorForm.getName();
            String surname = advisorForm.getSurname();
            LOGGER.log(Level.INFO, "Displays the form to search the Advisors if it doesn't exist\n" + " nom : "+ advisorForm.getName());
            List<Advisor> ListAdvisors = service.searchAdvisor(name,surname);
            model.addAttribute("listAdvisors", ListAdvisors);
            return "resultSearch";
        }else {
           model.addAttribute("searchAdvisorFrom", new SearchAdvisorForm());
           model.addAttribute("message", messageSource.getMessage("controller.search.fail", new Object[] {}, null));
           return "advisor.search";
        }
    }
 
 
}
-JSP :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Search an Advisor</title>
</head>
<body>
    <h1>Rechercher un conseiller</h1>
    <form:form method="POST" action="searchOneAdvisor" modelAttribute="searchAdvisorForm">
        <table id="searchbar">
            <tr>
 
                <td><label for="name">Nom : </label></td>
                <td><form:input id="name" path="name" type="text" placeholder="exemple : Doe"/></td>
                <td><label for="surname">Prenom : </label></td>
                <td><form:input id="surname" path="surname" placeholder = "exemple : John"/></td>
                <td><input type="submit" value="Search"/></td>
            </tr><br>
        </table>
    </form:form>
    <p align="center">${message}</p>
</body>
</html>
-modèle du formulaire

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 
package edu.sodteam.form;
 
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
 
import org.hibernate.validator.constraints.NotEmpty;
 
public class SearchAdvisorForm {
 
    @Pattern(regexp="^[A-Za-z0-9]+$", message="vous ne pouvez pas saisir de caractères spéciaux ou de chiffres")
    @NotNull
    String name;
 
    @Pattern(regexp="^[A-Za-z0-9]+$", message="vous ne pouvez pas saisir de caractères spéciaux ou de chiffres")
    @NotNull
    String surname;
 
    public String getName() {
        return this.name;
    }
 
    public String getSurname() {
        return this.surname;
    }
}