gpt4 book ai didi

java - JEE Spring 提交表单不适用于 POST 请求

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:00 26 4
gpt4 key购买 nike

我正在使用 Spring,我想使用 POST 方法提交我的 agentForm,但在表单提交时不会调用 submitNewAgent 。但是,当我用 GET 替换 POST 时,它会起作用。我已经研究了几天了,我不知道该改变什么。有人可以帮助我吗?

这是我的文件

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链接代理 View 和代理模型。

@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";
}

}

很抱歉混合了英语和法语。

最佳答案

<!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>

关于java - JEE Spring 提交表单不适用于 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com