【发布时间】:2017-01-07 17:57:12
【问题描述】:
我正在使用 Spring mvc web 提交表单。表单呈现正常但是当我提交此表单时,我收到 http 状态代码 400,但是如果我使用 firefox 的“编辑和重新发送”功能,它会点击相应的控制器方法(当我提交此表单时不会发生这种情况来自 html)。但是还有另一个问题,在该控制器方法中,当我检查我的 modelAttribute 对象时,我发现该对象中的所有内容都为空。情况并非如此,因为我在此表单中提交值。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="include.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>${message}</title>
</head>
<body>
General:
<br>
<form:form modelAttribute="initialMatchConfiguration" action="../match/configure" method="post" >
Date and time:
<form:input path="dateAndTimeOfMatch" type="text" />
<br> Match Type:
<form:select path="matchType">
<option value="friendly">friendly</option>
<option value="international">international</option>
<option value="domestic">domestic</option>
<option value="local">local tournament</option>
</form:select>
<br> Match Rules Configuration:
<form:select path="matchConfigurationId">
<option value="0">Random</option>
<option value="1">T20</option>
<option value="2">One Day</option>
<option value="3">Test</option>
</form:select>
<button type="button">Create a new rule configuration</button>
<br> Tournament:
<form:input type="text" path="tournament" />
<form:hidden path="tournamentId"/>
<button type="button">Create a new Tournament</button>
<br> Played at Stadium:
<form:input type="text" path="stadium" />
<form:hidden path="stadiumId"/>
<button type="button">Create a new stadium</button>
<br> <input type="submit" value="Save" />
</form:form>
</body>
</html>
下面的方法是渲染表单:
@RequestMapping(value = "/match/create", method = RequestMethod.GET)
public String createMatch(Model model){
model.addAttribute("initialMatchConfiguration", new InitialMatchConfiguration());
return "config-match-initial";
}
以下方法是提交表单:
@RequestMapping(value = "/match/configure", method = RequestMethod.POST)
public String configureMatch(@ModelAttribute("initialMatchConfiguration") InitialMatchConfiguration initialMatchConfig, Model model){
System.out.println(initialMatchConfig);
return "config-match-team";
}
InitialMatchConfiguration 类
public class InitialMatchConfiguration {
private Date dateAndTimeOfMatch;
private String matchType;
private Long matchConfigurationId;
private Long tournamentId;
private Long stadiumId;
private String tournament;
private String stadium;
public Date getDateAndTimeOfMatch() {
return dateAndTimeOfMatch;
}
public void setDateAndTimeOfMatch(Date dateAndTimeOfMatch) {
this.dateAndTimeOfMatch = dateAndTimeOfMatch;
}
public String getMatchType() {
return matchType;
}
public void setMatchType(String matchType) {
this.matchType = matchType;
}
public Long getMatchConfigurationId() {
return matchConfigurationId;
}
public void setMatchConfigurationId(Long matchConfigurationId) {
this.matchConfigurationId = matchConfigurationId;
}
public Long getTournamentId() {
return tournamentId;
}
public void setTournamentId(Long tournamentId) {
this.tournamentId = tournamentId;
}
public Long getStadiumId() {
return stadiumId;
}
public void setStadiumId(Long stadiumId) {
this.stadiumId = stadiumId;
}
public String getTournament() {
return tournament;
}
public void setTournament(String tournament) {
this.tournament = tournament;
}
public String getStadium() {
return stadium;
}
public void setStadium(String stadium) {
this.stadium = stadium;
}
}
从 firefox 编辑和重新发送工具触发时的请求:
请求标头:
POST http://localhost:8090/scoreboard.web/score/match/configure
Host: localhost:8090
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8090/scoreboard.web/score/match/create
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
请求正文:
Content-Type: application/x-www-form-urlencoded
Content-Length: 107
dateAndTimeOfMatch=&matchType=friendly&matchConfigurationId=0&tournament=&tournamentId=&stadium=&stadiumId=
【问题讨论】:
-
分享作为请求的一部分发送的请求正文以进一步评论
-
用请求/响应更新了问题。
-
勾选这一项 --> 将类型改为 type="date"
-
更新了答案。有用吗??
标签: html spring-mvc model-view-controller controller spring-web