我试图在表单提交中发布一组关键字。当字段为空或数据为空时,控制器接受它,但是当我尝试使用填充的表单数据提交时,其抛出错误表示"客户端发送的请求在语法上是不正确的。"错误代码为" 400"。 对我来说,很难跟踪它是在我的控制器上还是在视图中的错误?
我已经包含了我的代码的控制器,模型和视图。提前谢谢
广告系列类
@Entity
@Table(name = "CAMPAIGN")
public class Campaign implements Serializable {
@Id
@Column(name = "ID")
private String campaignId;
@Column(name = "NAME", length = 50)
private String name;
@Column(name = "CLIENT_ID", length = 50)
private String clientId;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "START_DATE")
private Date startDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "END_DATE")
private Date endDate;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "CAMPAIGN_KEYWORDS", joinColumns = {
@JoinColumn(name = "CAMPAIGN_ID", referencedColumnName = "ID", nullable = false)},
inverseJoinColumns = {@JoinColumn(name = "KEYWORD_ID", referencedColumnName = "VALUE", nullable = false)})
private List<KeyWord> keywords;
关键字类
@Entity
@Table(name = "KEYWORD")
public class KeyWord implements Serializable {
@Id
@Column(name = "VALUE")
private String value;
@Column(name = "TYPE")
private String type;
@Column(name = "STATUS")
private String status;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "keywords")
private List<Campaign> Campaigns;
控制器类
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String edit(Model model, @RequestParam(value = "id", required = false, defaultValue = "0") String id) {
Campaign campaign;
if(id.equalsIgnoreCase("0"))
{
campaign = new Campaign();
}
else{
campaign = campaignDao.getCampaignById(id);
}
List<KeyWord> keyWordList = keyWordDoa.getAllKeywords();
model.addAttribute("campaign", campaign);
model.addAttribute("keyWordList", keyWordList);
return campaignURL + "edit";
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String save(Model model, @ModelAttribute(value = "campaign") Campaign campaign, HttpServletRequest request) {
if(campaign.getCampaignId() == null)
{
String id = GenerateID.getID();
campaign.setCampaignId(id);
campaign.setUrl(id);
}
campaignDao.addCampaign(campaign);
return "redirect:/campaign";
}
Thymeleaf观点
<form th:action="@{/campaign/edit}" method="post" th:object="${campaign}"
enctype="multipart/form-data">
<div class="form-group">
<label class="form-label">Campaign ID</label>
<div class="controls">
<input type="text" disabled="disabled" class="form-control"
th:field="*{campaignId}">
</div>
</div>
<div class="form-group">
<label class="form-label">Name</label>
<span class="help">e.g. "Red Bull"</span>
<div class="controls">
<input type="text" class="form-control" th:field="*{name}"
placeholder="Red Bull">
</div>
</div>
<div class="form-group">
<label class="form-label">User Group</label>
<span class="help">Select The User group who has access</span>
<div class="controls">
<select class="multi" style="width:100%" th:field="*{clientId}">
<option th:each="keyWord : ${keyWordList}"
th:value="${keyWord}" th:text="${keyWord.value}">Credit
card
</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="form-label">Start Date</label>
<span class="help">"YYYY/MM/DD"</span>
<div class="controls">
<div class="input-append success date">
<input type="date" class="form-control" th:field="*{startDate}">
<span class="add-on"><span class="arrow"></span>
<i class="fa fa-th"></i></span>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="form-label">End Date</label>
<span class="help">"YYYY/MM/DD"</span>
<div class="controls">
<div class="input-append success date">
<input type="date" class="form-control" th:field="*{endDate}">
<span class="add-on"><span class="arrow"></span>
<i class="fa fa-th"></i></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="form-label">Keywords</label>
<span class="help">e.g. "Energy, Drink, RedBull"</span>
<div class="controls">
<select class="multi" style="width:100%" multiple th:field="*{keywords}" th:items>
<option th:each="keyWord : ${keyWordList}"
th:value="${keyWord}" th:text="${keyWord.value}">Credit card
</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<button class="btn btn-default btn-cons" onclick="something()">Cancel
</button>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<button class="btn btn-success btn-cons" type="submit">Save</button>
</div>
</div>
</form>