【问题标题】:Passing thymeleaf information to a hidden form将百里香叶信息传递给隐藏表单
【发布时间】:2019-06-13 18:36:21
【问题描述】:

我正在尝试从 thymeleaf 列表中传递信息并尝试将其添加到数据库中。 我正在从 tmdb 获取数据,它会发生变化,因此我将获取的信息显示到端点“/LatestMovies”,此信息不会保存在数据库中,应该是以太。所以我想为客户添加一个保存按钮来添加列出的电影。(它很简单,它只有movieid和moviename) 显示列出的电影我没有问题,它工作正常,但我得到错误的地方是当我添加隐藏表单时。我目前的代码是这样的:

<div class="container">   
<table class="table table-hover">
    <tr>
        <th>Id</th>
        <th>Name</th>
    </tr>
    <tr th:each="LatestMovies : ${latestMovies}">
        <td th:text="${LatestMovies.id}"></td>
        <td th:text="${LatestMovies.movieName}"></td>
        <td>
         <form action="#" th:action="@{/LatestMovies}" th:object="${addMovies}" method="post">
    <p><input type="hidden" th:field="*{id}" th:attr="value = ${LatestMovies.id}" /></p>
    <p><input type="hidden" th:field="*{movieName}" th:attr="value = ${LatestMovies.movieName}" /></p>
    <p><input type="submit" value="Submit" /></p>
</form>
</td>

    </tr>
</table>

@Controller
public class LatestMoviesController {

@Autowired
private LatestMoviesDao listOfMovies;

@Autowired
private savedMoviesDao movieRepo;


@GetMapping("/LatestMovies")
public String prueba(Model model) {

    TmdbMovies movies = new TmdbApi("22914f477aaa3e7f86c6f5434df8d1eb").getMovies();
    ResultsPage<MovieDb> movie = movies.getPopularMovies("en", 1);

    for(int i=0; i <= 19; i++){
        int movieId = movie.getResults().get(i).getId();
        String movieName = movie.getResults().get(i).toString();
        listOfMovies.save(new LatestMovies(movieId, movieName));
        }

    model.addAttribute("latestMovies", listOfMovies.findAll());


    return "index";

}

 @PostMapping("/LatestMovies")
    public String save(@ModelAttribute("addMovies") Model model, SavedMovies addMovies) {

     movieRepo.save(addMovies);

        return "index";
    }


}

提前谢谢

【问题讨论】:

    标签: java hibernate spring-boot thymeleaf


    【解决方案1】:

    首先,让我们更改您的表单。您不需要向其中添加新对象,因为您已经在遍历它们的列表。这样,您还可以避免使用th:attr 手动添加每个字段的值。我们要做的是分别发送所需的参数,然后用它们构建我们的电影对象。

    <div class="container">   
        <table class="table table-hover">
           <tr>
              <th>Id</th>
              <th>Name</th>
           </tr>
           <tr th:each="LatestMovies : ${latestMovies}">
              <td th:text="${LatestMovies.id}"></td>
              <td th:text="${LatestMovies.movieName}"></td>
              <td>
                  <form th:action="@{/LatestMovies}" method="post">
                      <p><input type="hidden" th:value="${LatestMovies.id}" name="id"/></p>
                      <p><input type="hidden" th:value="${LatestMovies.movieName}" name="name"/></p>
                      <p><input type="submit" value="Submit"/></p>
                  </form>
              </td>
           </tr>
        </table>
    </div>
    

    现在,在您的控制器上进行以下修改。

    @PostMapping("/LatestMovies")
    public String save(@RequestParam("id") Integer id, @RequesParam("name") String name) {
        SavedMovies movie = new SavedMovies();
        movie.setId(id);
        movie.setName(name);
        movieRepo.save(movie);
        return "index";
    }
    

    这些更改应该可以解决问题。

    【讨论】:

    • 我通过将th:field 更改为th:value 来更新代码。它现在应该可以工作了。
    猜你喜欢
    • 2018-07-02
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2015-11-29
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多