简体   繁体   English

将百里香叶信息传递给隐藏形式

[英]Passing thymeleaf information to a hidden form

I am trying to pass the information from a thymeleaf list and trying to add it to database. 我正在尝试从百里香列表中传递信息,并尝试将其添加到数据库中。 I am getting data from the tmdb and it will be changing so i display the information obtain to the endpoint "/LatestMovies" this information is not saved in the db and ether should it be. 我正在从tmdb获取数据,并且它将不断变化,因此我将获取的信息显示到端点“ / LatestMovies”,该信息未保存在db中,应该保存在以太中。 so i am trying to add a save button for the custumer to add the movie listed.(its simple it just haves movieid and moviename) Showing the movies listed i have no problem and it works fine but where i get error is when i add a hidden form. 所以我想为custumer添加一个保存按钮以添加列出的电影。(它很简单,它只有movieid和moviename)显示列出的电影我没有问题,并且可以正常工作,但是当我添加一个电影时出现错误隐藏的形式。 The current code i have is this: 我目前的代码是这样的:

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


}

Thx in advance 提前Thx

First, let's change your form. 首先,让我们更改表格。 You don't need to add a new object to it, since you are already iterating through a list of them. 您不需要向其添加新对象,因为您已经在其中遍历了它们。 That way, you will also avoid having to add the value for each field manually using th:attr . 这样,您还可以避免必须使用th:attr为每个字段手动添加值。 What we are gonna do, is send the required params separately and then build our movie object with them. 我们要做的是分别发送所需的参数,然后用它们构建我们的电影对象。

<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>

Now, on your controller, do the following modifications. 现在,在您的控制器上,进行以下修改。

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

These changes should do the trick. 这些更改应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM