spring应用程序:使用@model属性将表单对象解析到控制器时出现问题错误org.attoparser.parseexception:field

bvjxkvbb  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(219)
<!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

            <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/bootstrap.min.css}">

            <title>Home</title>
        </head>
        <body class="p-3 mb-2 bg-light text-black">
            <div class="container">
                <div id="logoutDiv">
                    <h1 th:text="${'Welcome ' + name}">Name</h1>
                    <form action="#" th:action="@{/logout}"method="POST">
                        <button type="submit" class="btn btn-secondary float-right">Logout</button>
                    </form>
                </div>
                <div id="contentDiv" style="clear: right;">
                    <nav style="clear: right;">
                        <div class="nav nav-tabs" id="nav-tab" role="tablist">
                            <a class="nav-item nav-link active" id="nav-files-tab" data-toggle="tab" href="#nav-files" role="tab" aria-controls="nav-files" aria-selected="true">Files</a>
                            <a class="nav-item nav-link" id="nav-notes-tab" data-toggle="tab" href="#nav-notes" role="tab" aria-controls="nav-notes" aria-selected="false">Notes</a>
                            <a class="nav-item nav-link" id="nav-credentials-tab" data-toggle="tab" href="#nav-credentials" role="tab" aria-controls="nav-credentials" aria-selected="false">Credentials</a>
                        </div>
                    </nav>Upload<
                    <div class="tab-content" id="nav-tabContent">
                        <div class="tab-pane fade show active" id="nav-files" role="tabpanel" aria-labelledby="nav-files-tab">
                            <p th:text="${message}" th:if="${message ne null}" class="alert alert-primary"></p>
                            <form action="#" enctype="multipart/form-data" th:action="@{'/file/uploadFile'}" th:method="POST" >
                                <div class="container">
                                    <div class="row" style="margin: 1em;">
                                        <div class="col-sm-2">
                                            <label for="fileUpload">Upload a New File:</label>
                                        </div>
                                        <div class="col-sm-6">
                                            <input type="file" class="form-control-file" id="fileUpload" name="fileUpload">
                                        </div>
                                        <div class="col-sm-4">
                                            <button type="submit" class="btn btn-dark">/button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <div class="table-responsive">
                                <table class="table table-striped" id="fileTable">
                                    <thead>
                                        <tr>
                                            <th style="width: 20%" scope="col"></th>
                                            <th style="width: 80%" scope="col">File Name</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="file : ${files}">
                                        <td>
                                            <a target="_blank" class="btn btn-success"  th:href="@{/file/{filename}(fileName = ${file.filename})}">View</a>
                                            <a class="btn btn-danger" th:href="@{/file/delete{filename}(filename = ${file.filename})}" >Delete</a>
                                        </td>
                                        <th scope="row" th:text="${file.filename}" ></th>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="tab-pane fade" id="nav-notes" role="tabpanel" aria-labelledby="nav-notes-tab">
                            <button style="margin: 0.25em;" type="button" class="btn btn-info float-right" onclick="showNoteModal()">
                                + Add a New Note
                            </button>
                            <div class="table-responsive">
                                <table class="table table-striped" id="userTable">
                                    <thead>
                                    <tr>
                                        <th style="width: 20%" scope="col"></th>
                                        <th style="width: 20%" scope="col">Title</th>
                                        <th style="width: 60%" scope="col">Description</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="note : ${notes}">
                                        <td>
                                            <button id="edit-note" type="button" class="btn btn-success"
                                                    th:attr="data-id=${note.getNoteId},
                                                    data-title=${note.getNoteTitle},
                                                    data-description=${note.getNoteDescription}"
                                                    onclick="showNoteModal (this.getAttribute('data-id'),this.getAttribute('data-title'),this.getAttribute('data-description'))">Edit/View</button>
                                            <a id="delete-note" class="btn btn-danger" th:href="@{/note/delete/{noteId}(noteId = ${note.getNoteId()})}">Delete</a>
                                        </td>
                                        <th  id="notetitle" scope="row" th:text="${note.getNoteTitle()}">Example Title</th>
                                        <td id="notedescription" th:text="${note.getNoteDescription()}">Example Description </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>

                            <div class="modal fade" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="noteModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="noteModalLabel">Note</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
    -Here is the error that I get-
                                            <form action="#" method="POST" th:action="@{/note/add}" th:object="${Notes}">
                                                <input type="hidden" name="noteId" id="note-id">
                                                <div class="form-group">
                                                    <label for="note-title" class="col-form-label">Title</label>
                                                    <input type="text" name= "noteTitle" class="form-control" id="note-title"  maxlength="20" required th:field="*{noteTitle}">
                                                </div>
                                                <div class="form-group">
                                                    <label for="note-description" class="col-form-label">Description</label>
                                                    <textarea class="form-control" name="noteDescription" id="note-description" rows="5" maxlength="1000" required th:field="*{noteDescription}"></textarea>
                                                </div>
                                                <button id="noteSubmit" type="submit" class="d-none"></button>
                                            </form>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                            <button type="button" class="btn btn-primary" onclick="$('#noteSubmit').click();">Save changes</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="tab-pane fade" id="nav-credentials" role="tabpanel" aria-labelledby="nav-credentials-tab">
                            <button style="margin: 0.25em;" type="button" class="btn btn-info float-right" onclick="showCredentialModal()">
                                + Add a New Credential
                            </button>

                            <div class="table-responsive">
                                <table class="table table-striped" th:object="${credentials}" id="credentialTable">
                                    <thead>
                                        <tr>
                                            <th style="width: 20%" scope="col"></th>
                                            <th style="width: 35%" scope="col">URL</th>
                                            <th style="width: 20%" scope="col">Username</th>
                                            <th style="width: 25%" scope="col">Password</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <tr>
                                        <td>
                                            <button type="button" class="btn btn-success">Edit</button>
                                            <a class="btn btn-danger">Delete</a>
                                        </td>
                                        <th scope="row">Example Credential URL</th>
                                        <td>Example Credential Username</td>
                                        <td>Example Credential Password</td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>

                            <div class="modal fade" id="credentialModal" tabindex="-1" role="dialog" aria-labelledby="credentialModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="credentialModalLabel">Credential</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <form action="#" method="POST">
                                                <input type="hidden" name="credentialId" id="credential-id">
                                                <div class="form-group">
                                                    <label for="note-title" class="col-form-label">URL</label>
                                                    <input type="text" name= "url" class="form-control" id="credential-url" maxlength="100" required>
                                                </div>
                                                <div class="form-group">
                                                    <label for="note-title" class="col-form-label">Username</label>
                                                    <input type="text" name= "username" class="form-control" id="credential-username" maxlength="30" required>
                                                </div>
                                                <div class="form-group">
                                                    <label for="note-title" class="col-form-label">Password</label>
                                                    <input type="text" name= "password" class="form-control" id="credential-password" maxlength="30" required>
                                                </div>
                                                <button id="credentialSubmit" type="submit" class="d-none"></button>
                                            </form>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                            <button type="button" class="btn btn-primary" onclick="$('#credentialSubmit').click();">Save changes</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <script th:src="@{/js/jquery-slim.min.js}"></script>
            <script th:src="@{/js/popper.min.js}"></script>
            <script th:src="@{/js/bootstrap.min.js}"></script>

            <!--For opening the note modal-->
            <script type="text/javascript">
                // For opening the note modal
                function showNoteModal(noteId, noteTitle, noteDescription) {
                    $('#note-id').val(noteId ? noteId : '');
                    $('#note-title').val(noteTitle ? noteTitle : '');
                    $('#note-description').val(noteDescription ? noteDescription : '');
                    $('#noteModal').modal('show');
                }

                // For opening the credentials modal
                function showCredentialModal(credentialId, url, username, password) {
                    $('#credential-id').val(credentialId ? credentialId : '');
                    $('#credential-url').val(url ? url : '');
                    $('#credential-username').val(username ? username : '');
                    $('#credential-password').val(password ? password : '');
                    $('#credentialModal').modal('show');
                }
            </script>
        </body>
    </html>

-I get an error when trying to parse the Note Form into my Note Controller. I try to parse my model object Notes in the form in my HTML home form into the Controller but it can not except the fields from the form.  My first error is this:

原因:org.thymeleaf.exceptions.templateprocessingexception:执行处理器'org.thymeleaf.spring5.processor.springinputgeneralfieldtagprocessor'(模板:“home”-第109行,第148列)时出错,第109行是表单中的字段
我还得到了第二个错误:java.lang.illegalstateexception:bindingresult和bean名“notes”的普通目标对象都不能作为请求属性使用-

--- Note Controller ---

@Controller
@RequestMapping("note")
public class NoteController {

    private NoteService noteServices;
    private UserService userService;

    public NoteController(NoteService noteServices, UserService userService) {
        this.noteServices = noteServices;
        this.userService = userService;
    }

    // Add an new note

    @PostMapping("add")
    public String addNote(@ModelAttribute(value="Notes")Notes Notes, Authentication authentication, Model model) throws IOException {
        String userName = authentication.getName();
        User user = userService.getUser(userName);
        Integer userid = user.getId();

        if(Notes.getNoteId() == null){
            noteServices.addNote(Notes,userid);
        }else{
            noteServices.editNote(Notes);

        }

        return "result";

    }

    @GetMapping("/delete/{noteId:.+}")
    public String deleteNote(@PathVariable Integer noteId, Authentication authentication, RedirectAttributes redirectAttributes){
        noteServices.deleteNote(noteId);
        redirectAttributes.addFlashAttribute("deleteNoteSuccess","Note deleted successfully.");
        return "redirect:/result";
    }

    private Integer getUserId(Authentication authentication) {
        String userName = authentication.getName();
        User user = userService.getUser(userName);
        return user.getId();
    }

}

---

--- POJO Model ---

public class Notes  {

    private Integer noteId;
    private String noteTitle;
    private String noteDescription;
    private Integer userId;

    public Notes(String noteTitle, String noteDescription, Integer userId) {
        this.noteTitle = noteTitle;
        this.noteDescription = noteDescription;
        this.userId = userId;
    }

    public Integer getNoteId() {
        return noteId;
    }

    public void setNoteId(Integer noteId) {
        this.noteId = noteId;
    }

    public String getNoteTitle() {
        return noteTitle;
    }

    public void setNoteTitle(String noteTitle) {
        this.noteTitle = noteTitle;
    }

    public String getNoteDescription() {
        return noteDescription;
    }

    public void setNoteDescription(String noteDescription) {
        this.noteDescription = noteDescription;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题