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

时间:2017-10-23 15:15:23

标签: java spring forms

我使用的是Spring,我希望使用POST方法提交agentForm,但在表单提交时不会调用submitNewAgent。但是,当我将POST替换为GET时,它可以正常工作。我现在已经工作了几天,我不知道该改变什么。有人能帮帮我吗?

这是我的文件

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>
&#13;
&#13;
&#13;

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

}

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

1 个答案:

答案 0 :(得分:0)

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