Spring boot 何时在Spring Boot中使用@RequestMapping、@GetMapping和@PostMapping?
在我的主页上,我想用数据库中的数据填充这些表。 Page home.html:Spring boot 何时在Spring Boot中使用@RequestMapping、@GetMapping和@PostMapping?,spring-boot,Spring Boot,在我的主页上,我想用数据库中的数据填充这些表。 Page home.html: <div class="table-responsive"> <table class="table table-dark" id="userTable"> <thead>
<div class="table-responsive">
<table class="table table-dark" 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="nota:${notas}">
<td>
<button type="button" class="btn btn-success"
th:onclick="javascript:showNoteModal(${nota.getId()},${nota.getTitle()},${nota.getDescription()})">Edit</button>
<a class="btn btn-danger"
th:href="${/eliminarNota(notaId=${nota.getId()})}">Delete</a>
</td>
<th scope="row" th:text="${nota.getTitle()}">Example Note Title</th>
<td th:text="${nota.getDescription()}">Example Note Description </td>
</tr>
</tbody>
</table>
</div>
使用诸如thymeleaf之类的服务器渲染框架时,其控制器可分为两组,以便更好地理解:
- 控制器向客户端显示页面内容
- 控制器从客户端接收操作
@Controller
@RequestMapping({"/home" })
public class ViewController {
@Autowired
private StudentService service;
@GetMapping
public String viewHomePage(Model model) {
List<Student> studentList = service.listAll();
model.addAttribute("studentList", liststudent);
model.addAttribute("someField", "foo");
return "home";
}
}
样品
@Controller
@RequestMapping({"/home" })
public class ViewController {
@Autowired
private StudentService service;
@GetMapping
public String viewHomePage(Model model) {
List<Student> studentList = service.listAll();
model.addAttribute("studentList", liststudent);
model.addAttribute("someField", "foo");
return "home";
}
}
@Controller
public class FormController {
@PostMapping("/save")
public String submissionResult(@ModelAttribute("personForm") Person person) {
return "result";
}
}