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

时间:2018-03-29 15:33:31

标签: java spring spring-boot thymeleaf

我的应用程序无法将表单值从基于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

2 个答案:

答案 0 :(得分:1)

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

Spring model attributes

Handling the command object

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

将您的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;
}

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

答案 1 :(得分:1)

基于SHEIX的上述输入做了一些改动并且有效。

<强> ShowFormController.java

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

<强> show.html

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

<强> Message.java

import lombok。*;

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

<强> PublishMessageController.java

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

<强> result.html

databaseMaintenance.child(uid).removeValue();