Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:将两个POST字段映射到三个结果字段_Spring_Thymeleaf - Fatal编程技术网

Spring:将两个POST字段映射到三个结果字段

Spring:将两个POST字段映射到三个结果字段,spring,thymeleaf,Spring,Thymeleaf,在我春季旅行的后“你好世界”阶段。创建一个应用程序,其中在三个字段中发布结果的两个字段 我的控制器有三个字段。其中两个在POST上填充,所有三个都应填充结果字段 控制员: @Slf4j @Controller public class GreetController { @GetMapping("/greeting") public String greetingForm(Model model) { model.addAttribute(&

在我春季旅行的后“你好世界”阶段。创建一个应用程序,其中
在三个字段中发布
结果的两个字段

我的控制器有三个字段。其中两个在POST上填充,所有三个都应填充结果字段

控制员:

@Slf4j
@Controller
public class GreetController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
        model.addAttribute("greeting", greeting);
    }
}
模型:

public class Greeting {

    //fields correspond to greeting.html
    private long id;
    private String content;
    private String numbah;

    public String getNumbah() { return numbah; }
    public void setNumbah() {
        this.numbah = SomeFunctions.functionOne(this.getContent());
    }

    public long getId() { return id; }
    public void setId(long id) { this.id = id; }

    public String getContent() { return content; }
    public void setContent(String content) { this.content }

}
观点:

<!DOCTYPE HTML>
this is greeting.html
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<!--th.fields correspond to fields in th.object (ie greeting.java)-->
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Message: <input type="text" th:field="*{content}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<p th:text="'numbah: ' + ${greeting.numbah}" />

<a href="/greeting">Submit another message</a>
</body>
</html>
…字段正在填充,因此函数本身…函数

长话短说,我在这里试图做的是获取
id
content
,执行
someFunction(content)=numbah
并返回
id
content
,和
numbah


提前谢谢你。

事情总是简单的,不是吗

模型中的
setNumbah
构造函数中有一个输入错误

这就是为什么我很少在这样的网站上发帖的原因:因为如果你只是睡一觉,再看一眼,你几乎总能发现自己做错了什么


感谢您查看此内容,并对这不是一个“高质量”问题/答案表示歉意。如果这是自答问题的协议,则将删除。

无需删除。自我回答是可以的。
<body>
<h1>Form</h1>
<!--th.fields correspond to fields in th.object (ie greeting.java)-->
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Message: <input type="text" th:field="*{content}" /></p>
    <p>Some Number: <input type="text" th:field="*{numbah}"></p>

    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<p th:text="'numbah: ' + ${greeting.numbah}" />
<a href="/greeting">Submit another message</a>
</body>