繁体   English   中英

无法将基于Thymeleaf的UI表单中的值绑定到Spring Boot中的控制器

[英]Not able to bind values from Thymeleaf based UI form to a controller in Spring Boot

我的应用程序无法将表单值从基于Thymeleaf + HTML的UI绑定到Spring Boot控制器。

当我在控制器中执行System.out.println时,该值为null

的index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Getting Started: Serving Web Content</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        <p>Get your greeting <a href="/greeting">here</a></p>
        <form action="/publishMessage" method="post">
            <table>
                <tr>
                    <th>CHARGE</th>
                </tr>
                <tr>
                    <td>
                        <textarea th:field="*{messageBody}" name="" cols="90" rows="40">
                        {
                        "firstname": "Jose",
                        "lastname": "Long"
                        }
                        </textarea>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" name="btnSubmit" value="Publish CHARGE message"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

PublishMessageController.java

@Controller
public class PublishMessageController {    
    @PostMapping("/publishMessage")
    public String publishMessage(Message message, BindingResult bindingResult, Model model) {
        System.out.println("into the publishMessage method..........");
        String messageBody = message.getMessageBody();
        System.out.println("messageBody: " + messageBody);
        return "result";
    }
}

Message.java

import lombok.*;

@Data
@Getter
@Setter
@NoArgsConstructor
public class Message {
    private String messageBody;
}

输出:

into the publishMessage method..........
messageBody: null

您的消息永远不会放入模型中,因此您不能在控制器中将其用作变量。

弹簧模型属性

处理命令对象

顺便说一句:该方法不应返回“结果”,而是消息正文的字符串。

将您的messageBody放置在显示表单的模型上:

@RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
  ... 
  String messageBody = ...
  model.addAttribute("messageBody", messageBody);
  ...
}

要在您的视图中使用它,请将th:actionth:object到您的表单中:

<form action="#" th:action="@{/publishMessage}" th:object="${messageBody}" method="post">
...
</form>

现在,您可以通过参数中的注释在控制器方法中使用它:

@PostMapping("/publishMessage")
public String publishMessage(@ModelAttribute(value="messageBody") String messageBody, BindingResult bindingResult, Model model) {
     ...
     return messageBody;
}

当然,您可以使用整个消息而不是正文来执行此操作。

基于SHEIX的上述输入,进行了一些更改并奏效。

ShowFormController.java

@Controller
public class ShowFormController {

    @RequestMapping(value = "/showForm", method = RequestMethod.GET)
    public String showForm(Model model) {
        model.addAttribute("message", new Message());
        return "show";
    }

}

show.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>
    <a href="/showForm">Go Back</a>
</h3>
<form action="#" th:action="@{/publishMessage}" th:object="${message}" method="post">
    <table>
        <tr>
            <th>CHARGE</th>
        </tr>
        <tr>
            <td>
                <textarea th:field="*{messageBody}" name="" cols="90" rows="40" style="background-color: beige">

                </textarea>
            </td>
        </tr>
        <tr>
            <td><input type="submit" name="btnSubmit" value="Publish CHARGE message"></td>
        </tr>
    </table>
</form>
</body>
</html>

Message.java

进口龙目岛。*;

@Data
@Getter
@Setter
@NoArgsConstructor
public class Message {
    private String messageBody = "{\n" +
            "\t\t\t\t\t\t\"name\": \"John\"\n" +
            "                    }";
}

PublishMessageController.java

@PostMapping("/publishMessage")
public String publishMessage(@ModelAttribute(value = "messageBody") String messageBody, BindingResult bindingResult, Model model) {
    System.out.println("into the publishMessage method..........");
    System.out.println("messageBody: " + messageBody);
    return "result";

result.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>
    Thanks !!!
    <a href="/">Go Back</a>
</h3>
</body>
</html>

暂无
暂无

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

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