ฟิลด์จะเป็นโมฆะเสมอในวิธี Spring Boot Postmapping

ฉันต้องการผูกตาราง HTML ของฉันกับทุกฟิลด์กับโค้ด Java ใน Spring Boot ดังนั้นฉันจึงใส่คำอธิบายประกอบวิธีการของฉันด้วย Postmapping ด้วยเช่นกัน

ฉันสามารถแสดงฟิลด์ทั้งหมดใน Thymeleaf ได้แล้วและยังสามารถตั้งค่าช่องทำเครื่องหมาย ( true / false ) ตามลำดับ

นี่คือโค้ด HTML ของ Thymeleaf ของฉัน:

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

And this is my Java code, where the field isActive of UserCreationDto is always null.

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

My 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 และชื่อผู้ใช้ จะถูกกรอกลงในโค้ด Java ของฉัน อย่างไรก็ตาม isactive จะเป็นโมฆะเสมอ

บางทีบางคนสามารถบอกฉันว่าฉันทำอะไรผิดที่นี่

ขอบคุณล่วงหน้า.


person Bernd    schedule 23.12.2018    source แหล่งที่มา
comment
แล้วการลองใช้ REST API ของคุณกับบุรุษไปรษณีย์เพื่อให้แน่ใจว่า Spring Boot Application ทำงานได้ดีล่ะ   -  person IEE1394    schedule 24.12.2018
comment
ฉันได้ทำเช่นนั้นแล้ว แต่เมื่อส่งคำขอ ฉันมักจะได้รับหน้าเข้าสู่ระบบในตอนแรก ฉันไม่คุ้นเคยกับ Spring Boot และ Postman มากนัก และฉันยังคงเรียนรู้อยู่   -  person Bernd    schedule 24.12.2018
comment
ดังนั้นเพิ่ม basicauth ให้กับการกำหนดค่าความปลอดภัยของสปริงแล้วลองใช้กับบุรุษไปรษณีย์ก่อน ... ถ้า REST API ใช้งานได้มากกว่าที่คุณจะจัดการส่วนหน้าได้ .. ไม่เช่นนั้นมันจะยาก ... อีกสิ่งหนึ่งที่อาจเป็นเครื่องมือสำหรับนักพัฒนาของเบราว์เซอร์ของคุณ .. คุณได้รับมันโดยการกด F12 สำหรับ chrome และ firefox ฉันเดาว่า .. หลังจากโหลดหน้าใหม่แล้วคุณจะเห็นคำขอของคุณซึ่งเบราว์เซอร์ส่งมา ...   -  person IEE1394    schedule 24.12.2018


คำตอบ (2)


ฉันคิดว่าคุณต้องตั้งค่าตัวเลือกมากมาย คุณไม่จำเป็นต้อง th:checked:

<input  th:field="*{users[__${itemStat.index}__].isActive}"
        class="checkBox"    
        type="checkBox"
        name="checkBox" />
person Simon Martinelli    schedule 23.12.2018
comment
ขอบคุณสำหรับความช่วยเหลือ แต่ฉันไม่มีปัญหากับโค้ด HTML ฉันคิดว่าปัญหาของฉันอยู่ที่โค้ด Java ของฉัน เมื่อส่งแบบฟอร์ม ฟิลด์ของฉันคือActive ภายใน @ModelAttribute UserCreationDto userTableSettings จะเป็นโมฆะเสมอ - person Bernd; 23.12.2018

ตอนนี้ฉันพบวิธีอื่นแล้ว แต่มันก็ไม่ค่อยดีนัก

@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}"
/>

ตอนนี้ปัญหาเดียวที่ฉันมีคือจะอัปเดตคอลัมน์ที่เป็นประเภท Boolean ใน 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);

อย่างไรก็ตาม ค่าในฐานข้อมูลของฉันไม่เคยเปลี่ยนแปลง

บางทีอาจมีคนให้คำแนะนำสุดท้ายแก่ฉันได้โปรด!

person Bernd    schedule 24.12.2018