提交后将用户发送到新页面

时间:2017-04-01 01:10:31

标签: spring-boot

提交新的剧集实体后,我需要将我的用户发送回仪表板 - 即/仪表板

我试过了

return /dashboard

但这只是将模板发送到浏览器而没有任何数据。

我的控制器看起来像这样:

public class formController {

private EpisodeRepository episodeRepository;

@Autowired
public void setEpisodeRepository(EpisodeRepository episodeRepository) {
    this.episodeRepository = episodeRepository;
}

@GetMapping("/newEpisode")
public String episodeForm(Model model) {
    model.addAttribute("episode", new Episode());
    return "episode";
}

@PostMapping("/newEpisode")
public String episodeSubmit(@ModelAttribute Episode episode) {
    episodeRepository.save(episode);
    return "dashboard";
}

@GetMapping("/dashboard")
public String dashboard(Model model) {
    List<Episode>listEpisodes = new ArrayList<>();
    episodeRepository.findAll().forEach((episode) -> {
        listEpisodes.add(episode);
            });
    System.out.println(listEpisodes.size());
    model.addAttribute("episodes", listEpisodes);
    return "dashboard";
}

@GetMapping("/event/{eventno}")
public String findEvent(@PathVariable String eventno, Model model) {
    Episode episode = new Episode();
    episode = episodeRepository.findByeventno(eventno);
    System.out.println("Printing the values held in episode for one get: " +eventno);
    model.addAttribute("episode", episode);
    return "episode";
    }
}

和html的相关部分

<form id="form" class="form-horizontal" method="post" action="#" th:action="@{/newEpisode}" th:object="${episode}">

1 个答案:

答案 0 :(得分:0)

答案是:

    @PostMapping("newEpisode")
public String episodeSubmit(@ModelAttribute Episode episode) {
    episodeRepository.save(episode);
    return "redirect:/dashboard";
}