I'm using Spring and I would like to submit my agentForm with POST
method but submitNewAgent
isn't called on form submit. However, it works when I replace POST
by GET
. I've been working on it for a few days now, and I have no idea what to change. Does anyone can help me please ?
Here my files
new.html
<!DOCTYPE html> <html lang="fr" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml" layout:decorator="index" > <head th:replace="index :: head"> <link href="../../static/css/bootstrap.min.css" rel="stylesheet" media="screen" /> </head> <body> <th:block layout:fragment="body"> <h1 class="page-header"> Nouvel Agent </h1> <form class="form" th:modelAttribute="agentForm" th:object="${agentForm}" action="/agents/ajouter-submit/" method="POST"> <div class="form-group"> <label>Prénom</label> <div th:if="${#fields.hasErrors('prenom')}" th:errors="*{prenom}" class="text-danger"> Erreur prénom </div> <input type="text" th:field="*{prenom}" class="form-control" /> </div> <div class="form-group"> <label th:for="*{nom}">Nom</label> <div th:if="${#fields.hasErrors('nom')}" th:errors="*{nom}" class="text-danger">Erreur nom</div> <input type="text" th:field="*{nom}" class="form-control" /> </div> <div class="form-group"> <button class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> <span th:remove="tag" th:text="#{label.add}"></span> </button> </div> </form> </th:block> </body> </html>
AgentForm.class
public class AgentForm {
@NotNull
@Size(min=2, max=255)
private String prenom;
@NotNull
@Size(min=2, max=255)
private String nom;
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Override
public String toString() {
return "Agent{" +
"prenom='" + prenom + '\'' +
", nom='" + nom + '\'' +
'}';
}
}
AgentController.class link agent views and agent model.
@Controller
@RequestMapping("/agents")
public class AgentController {
private final AgentService agentService;
@Autowired
public AgentController(AgentService agentService) {
this.agentService = agentService;
}
/**
* Gets all agents
*
* @param model view
* @return template name
*/
@RequestMapping(value = {"/", "lister"}, method = RequestMethod.GET)
public String allAgents(Model model) {
List<Agent> agentList = agentService.findAll();
if (agentList != null)
model.addAttribute("agentList", agentList);
return "agents/list";
}
/**
* Displays form to add an agent
*
* @return template and attributes
*/
@RequestMapping(value = {"/", "ajouter"}, method = RequestMethod.GET)
public ModelAndView addAgentForm() {
AgentForm a = new AgentForm();
a.setNom("test");
a.setPrenom("prenom");
return new ModelAndView("agents/new", "agentForm", a);
}
/**
* Manages the form to add an agent and submit in the repository
*
* @param agentForm form
* @param result results
* @param model form the view
* @param attributes view attributes
* @return url
*/
@RequestMapping(value = {"/", "ajouter-submit"}, method = RequestMethod.POST)
public String submitNewAgent(@ModelAttribute("agentForm") @Validated AgentForm agentForm,
BindingResult result, Model model, final RedirectAttributes attributes) {
if (result.hasErrors())
return "agents/new";
agentService.saveAndFlush(AgentAdapter.adaptAgentFormToAgent(agentForm));
attributes.addFlashAttribute("css", "success");
attributes.addFlashAttribute("msg", "L'agent est correctement ajouté !");
return "redirect:/agents/lister";
}
}
I'm sorry for mixing English and French.
<!DOCTYPE html> <html lang="fr" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml" layout:decorator="index" > <head th:replace="index :: head"> <link href="../../static/css/bootstrap.min.css" rel="stylesheet" media="screen" /> </head> <body> <th:block layout:fragment="body"> <h1 class="page-header"> Nouvel Agent </h1> <form class="form" th:modelAttribute="agentForm" th:object="${agentForm}" th:action="@{ajouter-submit}" method="POST"> <div class="form-group"> <label>Prénom</label> <div th:if="${#fields.hasErrors('prenom')}" th:errors="*{prenom}" class="text-danger"> Erreur prénom </div> <input type="text" th:field="*{prenom}" class="form-control" /> </div> <div class="form-group"> <label th:for="*{nom}">Nom</label> <div th:if="${#fields.hasErrors('nom')}" th:errors="*{nom}" class="text-danger">Erreur nom</div> <input type="text" th:field="*{nom}" class="form-control" /> </div> <div class="form-group"> <button class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> <span th:remove="tag" th:text="#{label.add}"></span> </button> </div> </form> </th:block> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.