【问题标题】:Field is always null in Spring Boot Postmapping methodSpring Boot Postmapping方法中的字段始终为空
【发布时间】:2019-05-23 04:00:03
【问题描述】:

我想在 Spring Boot 中将包含所有字段的 HTML 表绑定到 Java 代码。 因此,我也用 Postmapping 注释了我的方法。

我已经能够在 Thymeleaf 中显示所有字段,并且我还能够相应地设置复选框值 ( true / false )。

这是我的 Thymeleaf HTML 代码:

<form action="#" th:action="@{/mitarbeiterverwaltung}" th:object="${users}" method="post">
<fieldset>
    <table border="1" align="center">
        <thead>
            <tr>
                <!-- <th:text = "#{mitarbeiterverwaltung.active}>Active ( Tick ) / Passive</th>-->
                <th>Active ( Tick ) / Passive</th>
                <th>ID</th>
                <th>Username</th>
                <th>Anzeigename</th>
                <th>Dienstnummer</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="user, itemStat : *{users}">
                <td><input  th:field="*{users[__${itemStat.index}__].isActive}"
                            th:checked="${user.isActive}"
                            class="checkBox"    
                            type="checkBox"
                            name="checkBox" 

                /></td>
                <td><input  th:field="*{users[__${itemStat.index}__].id}" 
                            readonly/></td>
                <td><input  th:field="*{users[__${itemStat.index}__].username}" 
                            readonly/></td>
                <td><input  class="anzeigename" 
                            type="text" 
                            name="anzeigename" 
                            th:field="*{users[__${itemStat.index}__].anzeigename}" 
                            th:id="${itemStat.index}" 
                            readonly/></td>
                <td><input  class="dienstnummer" 
                            type="text" 
                            name="dienstnummer" 
                            th:field="*{users[__${itemStat.index}__].dienstnummer}" 
                            th:id="${itemStat.index}" 
                            readonly/></td>
            </tr>
        </tbody>
    </table>
    <br />
    <div style="text-align:center;">
        <input type="submit" id="submitButton" th:value="Speichern"/>
    </div>
</fieldset>

这是我的 Java 代码,其中 UserCreationDto 的 isActive 字段始终为空。

@PostMapping
public String updateActivePassiveUser(@ModelAttribute UserCreationDto userTableSettings,
        @RequestParam("checkBox") String checkBoxName, BindingResult result, Model model, Errors errors) {

    logger.info("Method {} called in {}", new Object() {}.getClass().getEnclosingMethod().getName(), this.getClass().getName());

    if (errors.hasErrors()) {
        logger.error("Error in {}", new Object() {}.getClass().getEnclosingMethod().getName());
        return "error";
    }

    List<Benutzer> users = userManagementServiceImpl.getAllUsers();

    userManagementServiceImpl.updateActivePassiveUser(1, 0);

    return "redirect:/mitarbeiterverwaltung?success";
}

这是Java代码中字段的图片,其中方法用@PostMapping注释

我的@RequestMapping 也是这样的:

这是我的@RequestMapping 方法:

@RequestMapping
public String showUserManagement(Model model) {
    logger.info("Method {} called in {}", new Object() {}.getClass().getEnclosingMethod().getName(), this.getClass().getName());
    List<Benutzer> users = userManagementServiceImpl.getAllUsers();
    userForm = userManagementServiceImpl.saveUserForm(users);
    model.addAttribute("users", userForm);
    return "mitarbeiterverwaltung";
}

我的 UserCreationDto 将所有字段添加到列表中:

public class UserCreationDto {

private List<User> users = new ArrayList<>();

public void addUser(User user) {
    this.users.add(user);
}

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}
}

我的简单 POJO 类包含所有字段

@Data

   public class User {
    //@SafeHtml prevents XSS ( Cross-Site Scripting )
    @SafeHtml
    private String username;
    private String password;
    private String anzeigename;
    private String dienstnummer;
    private long id;
    private Boolean isActive;
}

anzeigename、dienstnummer、id 和 username 等其他字段填充在我的 Java 代码中,但是,isactive 始终为 null。

也许,有人可以告诉我我在这里做错了什么。

非常感谢您。

【问题讨论】:

  • 如何尝试使用 Postman 的 REST API 以使舒尔 Spring Boot 应用程序正常工作?
  • 我已经这样做了,但是在发送我的请求时,我总是首先得到我的登录页面。我不太习惯 Spring Boot 和 Postman,我还在学习。
  • 所以将 basicauth 添加到您的 spring 安全配置中,并首先使用邮递员进行尝试...如果 REST API 有效,那么您可以使用前端...否则会很困难...另一件事可以成为您浏览器的开发人员工具...我猜您可以通过按 F12 来获取 chrome 和 firefox .. 重新加载页面后,您可以看到浏览器发送的请求...

标签: spring-boot thymeleaf


【解决方案1】:

我认为您必须设置很多选项。你不需要 th:checked:

<input  th:field="*{users[__${itemStat.index}__].isActive}"
        class="checkBox"    
        type="checkBox"
        name="checkBox" />

【讨论】:

  • 感谢您的帮助,但我的 HTML 代码没有问题。我认为我的问题在于我的 Java 代码。提交表单时,@ModelAttribute UserCreationDto userTableSettings 中的我的字段 isActive 始终为空。
【解决方案2】:

我现在找到了另一种方法,但它不是很好。

@PostMapping
public String updateActivePassiveUser(@Valid @ModelAttribute("userForm") UserCreationDto userTableSettings,
        @RequestParam List<String> searchValues, BindingResult result, Model model, Errors errors) {

字段 searchValues 包含所有勾选的复选框。 这是我的看法:

<td><input  type="checkbox"
    name="searchValues"
    th:value="${user.id}"
    th:checked="${user.isActive}"
/>

现在,我遇到的唯一问题是如何更新 Postgresql 中布尔类型的列?

为了完成这项任务,我称之为:

userRepo.updateActivePassiveUser(new Boolean("False"), id);

@Modifying
@Query(value = "UPDATE benutzer SET active = :active WHERE id = :id", nativeQuery = true)
@Transactional
void updateActivePassiveUser(@Param("active") Boolean active, @Param("id") long id);

但是,我的数据库中的值永远不会改变。

也许,有人可以给我最后的提示,拜托!

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 2020-04-19
    • 2021-01-18
    • 2017-05-08
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多