gpt4 book ai didi

java - 如何在@PostMapping中返回值

转载 作者:行者123 更新时间:2023-12-02 06:00:11 25 4
gpt4 key购买 nike

我对 Spring boot 还很陌生,并且遇到了一些问题。我有汽车和文件,当我添加汽车数据并将其保存在数据库中时,想要上传通过 OneToOne 关系连接到汽车的文件,但是当在 @PostMapping("/saveAutomobile") 中保存汽车时,我无法获取此 carsId并将其传输到 @PostMapping("/uploadFile") 进行连接。你有什么建议吗?

添加汽车:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<title>New Brand</title>
<link th:href="@{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
<div class="container add shadow">
<h2>New Automobile</h2>
<form th:action="@{/automobile/saveAutomobile}" method="post"
th:object="${automobileForm}">
<div class="row">
<div class="column">
<div class="form-group shadow">
<label class="form-control-label" for="inputBrand"> Brand</label>
<input type="text"
class="form-control form-control-danger box-shadow"
id="inputBrand" th:field="*{brand}" name="brand"
required="required" />
</div>
<div class="form-group shadow">
<label class="form-control-label" for="inputModel"> Model</label>
<input type="text"
class="form-control form-control-danger box-shadow"
id="inputModel" th:field="*{model}" name="model"/>

</div>
</div>
<div style="text-align: center;">
<a th:href="@{/file/uploadFile} + ${automobile?.getId()}"> <input type="submit"
class="btn btn-default box-shadow shadow" />
</a>
</div>
</form>
</div>
<script th:src="@{resources/js/jquery-1.11.1.min.js}"></script>
<script th:src="@{resources/js/bootstrap.min.js}"></script>
</body>
</html>
@PostMapping("/saveAutomobile")
public String saveAutomobile(@Valid final AutomobileForm automobileForm, final BindingResult result) {

if (result.hasErrors()) {
LOG.error("SaveAutomobile Error: " + result);

return "automobile/add";
}

final Automobile automobile = automobileConverter.ConvertToEntity(automobileForm);
if (LOG.isDebugEnabled()) {
LOG.info("Saving client " + automobile);
}

automobileService.save(automobile);
automobileForm.setId(automobile.getId());
return "/files/uploadFile";
}

上传文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />

<title>Upload automobile documentation</title>
<link th:href="@{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
<div class="container upload shadow">
<h2>Upload automobile documentation</h2>
<form th:action="@{/uploadFile}" method="post"
enctype="multipart/form-data" id="singleUploadForm"
name="singleUploadForm" >
<div class="form-group shadow">
<label class="form-control-label" for="uploadfile">Upload
File:</label> <input id="singleFileUploadInput" type="file" name="file"
class="file-input form-control form-control-danger box-shadow" />
</div>
<button type="submit" class="btn btn-default box-shadow shadow"
id="btnSubmit">Upload</button>
</form>
<div class="upload-response">
<div id="singleFileUploadError"></div>
<div id="singleFileUploadSuccess"></div>
</div>
</div>
<script src="/js/main.js"></script>
</body>
</html>
@Autowired
private FileStorageService DBFileStorageService;

@PostMapping("/uploadFile")
public FileForm uploadFile(@RequestParam("file") MultipartFile file) {
File dbFile = DBFileStorageService.storeFile(file);

return new FileForm(dbFile.getFileName(),
file.getContentType(), file.getSize());
}

最佳答案

您可以使用模型来存储数据并在 HTML 页面中使用它例如:-

Controller.java

    @GetMapping("/goToViewPage")
public ModelAndView passParametersWithModelAndView() {
ModelAndView modelAndView = new ModelAndView("viewPage");
modelAndView.addObject("message", "Hello from the controller");
return modelAndView;
}

Test.html

    <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
</head>
<body>
<div>Web Application. Passed parameter : th:text="${message}"</div>
</body>
</html>

关于java - 如何在@PostMapping中返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55983090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com