繁体   English   中英

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

[英]JEE Spring submit form doesn't work with POST request

我正在使用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链接代理视图和代理模型。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM