我在Spring MVC应用程序中从百万美元表单中检索值时遇到问题。这个应用程序有多种形式,除了一个工作正常。失败的一个稍有不同,因为它有一个从对象列表中提供的下拉列表。 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代码段:
<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>
&#13;
问题是当POST处理程序收到musicWish时,所有字段仍然为null。 相同的模式在其他形式中工作正常,但在这里失败...即使我评论下拉列表。我很感激任何帮助,以确定我在这里放置的错误。
THX,
的Stefan
答案 0 :(得分:0)
您的视图模型属性没有任何设置器,您需要为要绑定的字段提供公共设置器
答案 1 :(得分:0)
您的MusicWish和MusinGenre类不包含任何公共设置方法来设置值。
我建议您使用LOMBOK jar编写更简洁的代码。这避免了您在课堂上提及getter和Setter以及Constructors。但是,仅定义@Getter
,@Setter
,@NoArgsConstructor
和@AllArgsConstructor
,它将为您提供所有功能。例如,
@Entity
@Table(name = "musicgenre")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class MusicGenre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String genre;
}