【发布时间】:2018-04-13 15:28:11
【问题描述】:
我在 Spring MVC 应用程序中从 thymeleaf 表单中检索值时遇到问题。这个应用程序有多种形式,除了一种之外都可以正常工作。失败的稍有不同,因为它有一个从对象列表提供的下拉列表。 UI 工作正常,问题是 POST 不包含模型属性中的任何数据。
这是两个 POJO,第一个是下拉列表后面的:
@Entity
@Table(name = "musicgenre")
public class MusicGenre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String genre;
public MusicGenre() {
}
public MusicGenre(String genre) {
this.genre = genre;
}
public Integer getId() {
return id;
}
public String getGenre() {
return genre;
}
@Override
public String toString() {
return "MusicGenre{" +
"id=" + id +
", genre='" + genre + '\'' +
'}';
}
}
还有要填写的:
@Entity
@Table(name = "musicwish")
public class MusicWish {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String artist;
private String song;
private Integer genreId;
private String notes;
public MusicWish() {
}
public Integer getId() {
return id;
}
public String getArtist() {
return artist;
}
public String getSong() {
return song;
}
public Integer getGenreId() {
return genreId;
}
public String getNotes() {
return notes;
}
@Override
public String toString() {
return "MusicWish{" +
"id=" + id +
", artist='" + artist + '\'' +
", song='" + song + '\'' +
", genreId=" + genreId +
", notes='" + notes + '\'' +
'}';
}
private MusicWish(final Builder builder) {
this.artist = builder.artist;
this.song = builder.song;
this.genreId = builder.genreId;
this.notes = builder.notes;
}
public static class Builder {
private String artist;
private String song;
private Integer genreId;
private String notes;
public Builder withArtist(final String artist) { this.artist = artist; return this; }
public Builder withSong(final String song) { this.song = song; return this; }
public Builder withGenreId(final Integer genre) { this.genreId = genre; return this; }
public Builder withNotes(final String notes) { this.notes = notes; return this; }
}
}
我省略了服务类,因为它工作正常。控制器:
@Controller
public class MusicController {
@Autowired
private MusicService musicService;
@RequestMapping(path = "/music-wishlist", method = RequestMethod.GET)
public ModelAndView getMusicWishlist(Model model) {
ModelAndView modelAndView = new ModelAndView("music-wishlist");
modelAndView.addObject("wishes", musicService.getWishes());
modelAndView.addObject("genres", musicService.getGenreMap());
return modelAndView;
}
@RequestMapping(path = "/music-wish", method = RequestMethod.GET)
public ModelAndView getMusicWish(Model model) {
ModelAndView modelAndView = new ModelAndView("music-wish");
modelAndView.addObject("musicWish", new MusicWish());
modelAndView.addObject("genreList", musicService.getGenres());
return modelAndView;
}
@RequestMapping(path = "/music-wish", method = RequestMethod.POST)
public ModelAndView postMusicWish(
@ModelAttribute("musicWish") MusicWish musicWish) {
ModelAndView modelAndView = new ModelAndView("music-wishlist");
modelAndView.addObject("wishes", musicService.getWishes());
modelAndView.addObject("genres", musicService.getGenreMap());
return modelAndView;
}
}
最后是 HTML sn-p 格式:
<form th:action="@{/music-wish}"
th:object="${musicWish}"
method="post"
class="registration-text">
<p class="registration-input-label"
th:text="#{music.artist}"></p>
<input type="text"
class="registration-input-text"
th:field="*{artist}"/>
<p class="registration-input-label"
th:text="#{music.song}"></p>
<input type="text"
class="registration-input-text"
th:field="*{song}"/>
<p class="registration-input-label"
th:text="#{music.genre}"></p>
<select th:field="*{genreId}">
<option
th:each="genre: ${genreList}"
th:value="${genre.id}"
th:text="${genre.genre}" ></option>
</select>
<p class="registration-input-label"
th:text="#{music.notes}"></p>
<input type="text"
class="registration-input-text"
th:field="*{notes}"/>
<p style="margin-top: 20px;">
<input type="submit" th:value="#{button.submit}" />
<input type="reset" th:value="#{button.reset}" />
</p>
</form>
问题是当 POST 处理程序接收到 musicWish 时,所有字段仍然为空。 相同的模式在其他形式中也可以正常工作,但在这里失败......即使我将下拉列表注释掉。对于识别我在这里放置的错误的任何帮助,我将不胜感激。
谢谢,
斯蒂芬
【问题讨论】:
标签: java spring spring-mvc thymeleaf