Issue
I have simple spring boot app, which have a page to add some notes. enter image description here As long as I am login I can see it but when I log out and login again I don't until I add the new note. How to make those objects visible directly after re-login?
I am using H2 DB and my SpringSecurtity config looks like this:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/signup","/css/**", "/js/**").permitAll().
anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll();
http.formLogin().defaultSuccessUrl("/home", true);
http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
http.csrf().disable(); // in order to log into h2 console in browser
http.headers().frameOptions().disable(); // in order to log into h2 console in browser
}
HTML file for Notes handling:
<div class="tab-pane fade" id="nav-notes" role="tabpanel" aria-labelledby="nav-notes-tab">
<button id="add-new-note-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 type="button" class="btn btn-success">Edit</button>
<a class="btn btn-danger">Delete</a>
</td>
<th scope="row" th:text="${note.noteTitle}">Example Note Title</th>
<td th:text="${note.noteDescription}">Example Note 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">×</span>
</button>
</div>
<div class="modal-body">
<form action="#" th:object="${noteForm}" th:action="@{/home}" method="POST">
<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" field="*{noteTitle}" name= "noteTitle" class="form-control" id="note-title" maxlength="20" required>
</div>
<div class="form-group">
<label for="note-description" class="col-form-label">Description</label>
<textarea class="form-control" field="*{noteDescription}" name="noteDescription" id="note-description" rows="5" maxlength="1000" required></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 id="note-save-changes" type="button" class="btn btn-primary" onclick="$('#noteSubmit').click();">Save changes</button>
</div>
</div>
</div>
</div>
</div>
Solution
Problem solved. The reason for not dispplaying objects after re-login was the controller class - it didn't load needed data:
missing part in Controller class was loading data from the model:
model.addAttribute("notes", noteService.getUserNotes(userId));
Answered By - Krzysztof Pagacz
Answer Checked By - Pedro (JavaFixing Volunteer)