更新”电影”数据时出现问题,使用Thymeleaf和Spring。

huangapple go评论110阅读模式
英文:

Problem with updating "movie" data using Thymeleaf and spring

问题

以下是您提供的代码的翻译:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <form th:object="${movieToUpdate}" method="post">
  9. <p>电影名称: <input type="text" th:field="*{name}"></p>
  10. <p>电影描述: <input type="text" th:field="*{description}"></p>
  11. <img th:src="${movieToUpdate.getImageUrl()}" th:width="300" th:height="300"><br>
  12. <p>新图片链接: <input type="text" th:field="*{imageUrl}" class="cloudinary-fileupload"></p>
  13. <a th:href="${'/movie/update/'+movieToUpdate.getId()}">更新</a><br>
  14. </form>
  15. </body>
  16. </html>
  1. package pl.fsaaa.filmapp.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.*;
  6. import pl.fsaaa.filmapp.model.Movie;
  7. import pl.fsaaa.filmapp.service.CloudinaryImageUploader;
  8. import pl.fsaaa.filmapp.service.MovieService;
  9. import java.io.IOException;
  10. import java.util.List;
  11. @Controller
  12. public class MovieController {
  13. private CloudinaryImageUploader cloudinaryImageUploader;
  14. private MovieService movieService;
  15. @Autowired
  16. public MovieController(MovieService movieService, CloudinaryImageUploader cloudinaryImageUploader) {
  17. this.movieService = movieService;
  18. this.cloudinaryImageUploader = cloudinaryImageUploader;
  19. }
  20. @GetMapping("/addmovie")
  21. public String getNewMovie(Model model) {
  22. model.addAttribute("newMovie", new Movie());
  23. return "addMoviePage";
  24. }
  25. @PostMapping("/add-movie")
  26. public String addMovie(@ModelAttribute Movie movie) throws IOException {
  27. cloudinaryImageUploader.saveImageToCloudinary(movie.getImageUrl());
  28. movie.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
  29. movieService.addMovie(movie);
  30. return "redirect:/addmovie";
  31. }
  32. @GetMapping("/movies")
  33. public String getAllMovies(Model model) {
  34. List<Movie> movieList = movieService.getAllMovies();
  35. model.addAttribute("allMovies", movieList);
  36. return "moviesPage";
  37. }
  38. @GetMapping("/movie/{id}")
  39. public String getMovieDetail(Model model, @PathVariable Long id) {
  40. Movie movieToUpdate = movieService.getMovieById(id);
  41. model.addAttribute("movieToUpdate", movieToUpdate);
  42. return "movieDetailsPage";
  43. }
  44. @RequestMapping(value = "/movie/update/{id}", method = {RequestMethod.GET, RequestMethod.PUT})
  45. public String updateMovie(@PathVariable Long id, @ModelAttribute Movie movieToUpdate){
  46. movieService.addMovie(movieToUpdate);
  47. return "redirect:/movies";
  48. }
  49. @RequestMapping(value = "/movie/delete/{id}", method = {RequestMethod.DELETE, RequestMethod.GET})
  50. public String deleteMovie(@PathVariable Long id){
  51. movieService.deleteMovie(id);
  52. return "redirect:/movies";
  53. }
  54. }

请注意,我已经将HTML和Java代码中的特殊字符进行了恢复,以使其可读。如果您需要进一步的帮助或有任何问题,请随时提出。

英文:

Hey I've got a problem with updating my object. I've got a table of movies where can i delete it or go to details. In details i wanted to have my "update" option. Im giving whole object to movieDetailPage, it shows object details in input fields but i cant update it. My "updateMovie" method doesnt see what i typed in input. For example im changing name from "Movie1" to "Movie2" and method still see "Movie1"

Here's my movieDetailsPage.html

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot; xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;title&gt;Title&lt;/title&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. &lt;form th:object=&quot;${movieToUpdate}&quot; method=post &gt;
  9. &lt;p&gt;Movie name: &lt;input type=&quot;text&quot; th:field=&quot;*{name}&quot;&gt;&lt;/p&gt;
  10. &lt;p&gt;Movie description: &lt;input type=&quot;text&quot; th:field=&quot;*{description}&quot;&gt;&lt;/p&gt;
  11. &lt;img th:src=&quot;${movieToUpdate.getImageUrl()}&quot; th:width=&quot;300&quot; th:height=&quot;300&quot;&gt;&lt;br&gt;
  12. &lt;p&gt;Nowy image url: &lt;input type=&quot;text&quot; th:field=&quot;*{imageUrl}&quot; class=&quot;cloudinary-fileupload&quot;&gt;&lt;/p&gt;
  13. &lt;a th:href=&quot;${&#39;/movie/update/&#39;+movieToUpdate.getId()}&quot;&gt;Update&lt;/a&gt;&lt;br&gt;
  14. &lt;/form&gt;
  15. &lt;/body&gt;
  16. &lt;/html&gt;

and my controller class

  1. package pl.fsaaa.filmapp.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.*;
  6. import pl.fsaaa.filmapp.model.Movie;
  7. import pl.fsaaa.filmapp.service.CloudinaryImageUploader;
  8. import pl.fsaaa.filmapp.service.MovieService;
  9. import java.io.IOException;
  10. import java.util.List;
  11. @Controller
  12. public class MovieController {
  13. private CloudinaryImageUploader cloudinaryImageUploader;
  14. private MovieService movieService;
  15. @Autowired
  16. public MovieController(MovieService movieService, CloudinaryImageUploader cloudinaryImageUploader) {
  17. this.movieService = movieService;
  18. this.cloudinaryImageUploader = cloudinaryImageUploader;
  19. }
  20. @GetMapping(&quot;/addmovie&quot;)
  21. public String getNewMovie(Model model) {
  22. model.addAttribute(&quot;newMovie&quot;,new Movie());
  23. // model.addAttribute(&quot;newImage&quot;,new File(&quot;&quot;));
  24. return &quot;addMoviePage&quot;;
  25. }
  26. @PostMapping(&quot;/add-movie&quot;)
  27. public String addMovie(@ModelAttribute Movie movie) throws IOException {
  28. cloudinaryImageUploader.saveImageToCloudinary(movie.getImageUrl());
  29. movie.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
  30. movieService.addMovie(movie);
  31. return &quot;redirect:/addmovie&quot;;
  32. }
  33. @GetMapping(&quot;/movies&quot;)
  34. public String getAllMovies(Model model) {
  35. List&lt;Movie&gt; movieList = movieService.getAllMovies();
  36. model.addAttribute(&quot;allMovies&quot;,movieList);
  37. return &quot;moviesPage&quot;;
  38. }
  39. @GetMapping(&quot;/movie/{id}&quot;)
  40. public String getMovieDetail(Model model, @PathVariable Long id) {
  41. Movie movieToUpdate = movieService.getMovieById(id);
  42. System.out.println(movieToUpdate);
  43. model.addAttribute(&quot;movieToUpdate&quot;, movieToUpdate);
  44. return &quot;movieDetailsPage&quot;;
  45. }
  46. @RequestMapping(value = &quot;/movie/update/{id}&quot;, method = {RequestMethod.GET,RequestMethod.PUT})
  47. // @PutMapping(&quot;movie/update/{id}&quot;)
  48. public String updateMovie(@PathVariable Long id, @ModelAttribute Movie movieToUpdate){
  49. // movieToUpdate = movieService.getMovieById(id);
  50. // System.out.println(movieToUpdate);
  51. movieService.addMovie(movieToUpdate);
  52. return &quot;redirect:/movies&quot;;
  53. }
  54. @RequestMapping(value = &quot;/movie/delete/{id}&quot;, method = {RequestMethod.DELETE,RequestMethod.GET})
  55. public String deleteMovie(@PathVariable Long id){
  56. movieService.deleteMovie(id);
  57. return &quot;redirect:/movies&quot;;
  58. }
  59. }

答案1

得分: 0

问题已解决。
我已将提交按钮添加到详细页面,而不是链接:

  1. <form th:action="'/movies/update/'+*{id}" th:object="${movieToUpdate}" method="put">
  2. <p>电影名称:<input type="text" th:field="*{name}"></p>
  3. <p>电影描述:<input type="text" th:field="*{description}"></p>
  4. <img th:src="${movieToUpdate.getImageUrl()}" th:width="300" th:height="300"><br>
  5. <p>新图片链接:<input type="text" th:field="*{imageUrl}" class="cloudinary-fileupload"></p>
  6. <p><input type="submit" value="更新"></p>
  7. </form>

并且稍微更改了编辑 "movie" 方法,因为之前的方法未将更改后的图像上传到 Cloudinary。

  1. @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET, RequestMethod.PUT})
  2. public String updateMovie(@PathVariable Long id, @ModelAttribute Movie movieToUpdate) throws IOException {
  3. movieToUpdate.setId(id);
  4. cloudinaryImageUploader.saveImageToCloudinary(movieToUpdate.getImageUrl());
  5. movieToUpdate.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
  6. movieService.addMovie(movieToUpdate);
  7. return "redirect:/movies/all";
  8. }
英文:

Problem solved.
I've added submit button to details page instead of link:

  1. &lt;form th:action=&quot;&#39;/movies/update/&#39;+*{id}&quot; th:object=&quot;${movieToUpdate}&quot; method=put &gt;
  2. &lt;p&gt;Movie name: &lt;input type=&quot;text&quot; th:field=&quot;*{name}&quot;&gt;&lt;/p&gt;
  3. &lt;p&gt;Movie description: &lt;input type=&quot;text&quot; th:field=&quot;*{description}&quot;&gt;&lt;/p&gt;
  4. &lt;img th:src=&quot;${movieToUpdate.getImageUrl()}&quot; th:width=&quot;300&quot; th:height=&quot;300&quot;&gt;&lt;br&gt;
  5. &lt;p&gt;New image url: &lt;input type=&quot;text&quot; th:field=&quot;*{imageUrl}&quot; class=&quot;cloudinary-fileupload&quot;&gt;&lt;/p&gt;
  6. &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Update&quot;&gt;&lt;/p&gt;
  7. &lt;/form&gt;

and changed update edited "movie" method a little bit, because previous one wasn't uploading changed image to cloudinary.

  1. @RequestMapping(value = &quot;/update/{id}&quot;, method = {RequestMethod.GET,RequestMethod.PUT})
  2. public String updateMovie(@PathVariable Long id, @ModelAttribute Movie movieToUpdate) throws IOException {
  3. movieToUpdate.setId(id);
  4. cloudinaryImageUploader.saveImageToCloudinary(movieToUpdate.getImageUrl());
  5. movieToUpdate.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
  6. movieService.addMovie(movieToUpdate);
  7. return &quot;redirect:/movies/all&quot;;
  8. }

huangapple
  • 本文由 发表于 2020年7月22日 23:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63037319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定