【发布时间】:2017-02-26 10:42:18
【问题描述】:
我在部分更新我的页面时遇到问题。 我用几句话解释了业务问题。我有用户个人资料,他可以在其中更改有关自己的任何信息 等:技能、个人信息、主要照片库。 一切正常,但我有一件烦人的事情,我的整个页面在添加等照片后刷新。
首先我显示主照片添加准确
<div th:fragment="photoDiv">
<img id="mainPhoto" th:src="@{/profile/main/img}" alt=""/>
<div>
<form id="mainPhotoForm" action="#" th:action="@{/profile/main/upload}"
enctype="multipart/form-data" method="post">
<label for="files"><img src="../../img/plus.png"/> Upload photo</label>
<input id="files" style="display:none;" type="file" accept="image/*" th:name="img"
onchange="document.getElementById('mainPhotoForm').submit();"/>
</form>
</div>
</div>
我的控制器
@PostMapping("/profile/main/img")
public String uploadingMianPhoto(@RequestParam("img") MultipartFile file,
@ModelAttribute("userDto") userDto user) {
String path = filesStrorage.storeFiles(file, getCurrentUserID());
if (!path.isEmpty()) {
Photo mainPhoto = new Photo();
mainPhoto.setFileName(file.getOriginalFilename());
mainPhoto.setPath(path);
user.setProfilePhoto(mainPhoto);
}
return "/profile/edit";
}
// Load main photo
@RequestMapping(value = "/profile/main/upload")
public @ResponseBody
byte[] mainPhotoResponse(@ModelAttribute("userDto") UserDto user) throws IOException {
Path path = Paths.get(user.getProfilePhoto().getPath());
return Files.readAllBytes(path);
}
我只想更新th:fragment="photoDiv" 而不是整个页面。
我尝试了 ajax 片段更新,但我没有使用 spring webflow,我应该添加 spring webflow 配置还是可以通过 jquery 来做到这一点?
所以在尝试之后
我添加了uploadingMianPhoto return "/profile/edit :: photoDiv",但它只返回我整个页面上的这个片段
请给我一些ajax post函数的例子,请给我一些解释
【问题讨论】:
标签: javascript jquery ajax spring-mvc thymeleaf