【问题标题】:Spring thymeleaf give error in post requestSpring thymeleaf 在发布请求中给出错误
【发布时间】:2018-10-20 01:45:54
【问题描述】:

我有这个简单的代码:这是我重定向到页面的控制器类

@Controller
public class SimpleController {
    @GetMapping("/nuovo-utente")
    public String viewInserisciUtente(Model model){
        model.addAttribute("nuovoUtente", new Utente());
        return "nuovo-utente";
    }

    @PostMapping("/nuovo-utente")
    public void memorizzaUtente(@ModelAttribute Utente utente){
        System.out.println(utente.getId());
    }
}

这是模型类。

public class Utente {
    private String id=null;
    private String citta=null;
    private String genere=null;
    private String data_nascita=null;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCitta() {
        return citta;
    }

    public void setCitta(String citta) {
        this.citta = citta;
    }

    public String getGenere() {
        return genere;
    }

    public void setGenere(String genere) {
        this.genere = genere;
    }

    public String getData_nascita() {
        return data_nascita;
    }

    public void setData_nascita(String data_nascita) {
        this.data_nascita = data_nascita;
    }
}

我的带有百里香叶的 html 页面是这样的:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Inserisci un nuovo utente</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/nuovo-utente}" th:object="${nuovoUtente}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Città: <input type="text" th:field="*{citta}" /></p>
        <p>Genere: <input type="text" th:field="*{genere}" /></p>
        <p>Data nascita: <input type="text" th:field="*{data_nascita}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

所以,正如我在标题中所说,当我尝试通过 post 请求提交表单时,这个简单的表单代码给了我错误。报错如上:

Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "nuovo-utente" - line 10, col 32)

你能对我说什么?一些帮助将不胜感激

【问题讨论】:

  • 回答对你有帮助吗?

标签: java spring post request thymeleaf


【解决方案1】:

您必须使用不同的 html 和 url。一个用于创建表单,另一个用于提交表单。您使用的是相同的网址。

【讨论】:

    【解决方案2】:

    首先在你的html页面中改变

    <html xmlns:th="http://www.thymeleaf.org">
    

    <html xmlns:th="http://www.w3.org/1999/xhtml">
    

    然后像这样写你的控制器类

    @PostMapping("/nuovo-utente")
       public String memorizzaUtente(@ModelAttribute("nuovoUtente") Utente utente) {
          System.out.println(utente.getId());
          return "any page";
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2019-10-05
      • 2021-10-24
      • 2016-09-15
      • 1970-01-01
      相关资源
      最近更新 更多