Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 何时在Spring Boot中使用@RequestMapping、@GetMapping和@PostMapping?_Spring Boot - Fatal编程技术网

Spring boot 何时在Spring Boot中使用@RequestMapping、@GetMapping和@PostMapping?

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>

在我的主页上,我想用数据库中的数据填充这些表。 Page home.html:

<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之类的服务器渲染框架时,其控制器可分为两组,以便更好地理解:

  • 控制器向客户端显示页面内容
  • 控制器从客户端接收操作
显示页面内容 这里通常使用@RequestMapping和@GetMapping。此控制器的主要目标是将html模板与来自任何地方(如数据库)的数据“合并”:

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

}
样品

谢谢。这很有帮助。我是Spring Boot的新手,我正在一个项目中练习。
@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";
    }

}