不存在必需的字符串参数“出发”
我的控制器:
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(
@RequestParam String departure,
@RequestParam String arrival,
@RequestParam String departureTime,
Model model) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
departureTime += " 00:00:00"; // departureTime = departureTime + " 00:00:00";
LocalDateTime date = LocalDateTime.parse(departureTime, formatter);
Iterable<BusFlight> selects = busFlightService.getAll();
List<BusFlight> busflights = busFlightService.search(departure, arrival, date);
if (selects != null && busflights != null) {
model.addAttribute("selects", selects);
model.addAttribute("busflights", busflights);
} else {
model.addAttribute("busflights", "no flights");
}
return "search";
}
我的FreeMarker模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"
</head>
<body style="background-color: #138496;">
<form action="/public/search" method="get">
<div class="container" style="margin-top: 200px">
<div class="row">
<p>Click every input.</p>
</div>
<div class="row">
<span>
<select name="departure" class="combobox input-large form-control" style="margin-right: 10px">
<#list selects! as select>
<option value="${select.id}">${select.departure}</option>
</#list>
</select>
</span>
<span>
<select name="arrival" class="combobox input-large form-control">
<#list selects! as select>
<option value="${select.id}">${select.arrival}</option>
</#list>
</select>
</span>
<span>
<input type="date" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" value="2018-08-20" name="departureTime"/>
</span>
<span>
<button type="button" class="btn btn-primary">Search</button>
</span>
</div>
</div>
<#if busflights??>
<#list busflights as busflight >
<table class="table table-hover table-dark">
<thead>
<tr>
<th scope="col">Departure</th>
<th scope="col">Arrival</th>
<th scope="col">DepartureTime</th>
</tr>
</thead>
<tbody>
<tr>
<td>${busflight.departure}</td>
<td>${busflight.arrival}</td>
<td>${busflight.departureTime}</td>
</tr>
</tbody>
</table>
</#list>
<#else >
No buses
</#if>
</form>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="/static/js/bootstrap-combobox.js"></script>
<script>
$(document).ready(function(){
$('.combobox').combobox()
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36251023-1']);
_gaq.push(['_setDomainName', 'jqueryscript.net']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
我的错误:
There was an unexpected error (type=Bad Request, status=400).
Required String parameter 'departure' is not present
还有一个问题,是否可以对一个映射“ / search”使用GET请求并将其放入客户端大小?就像我在案件中尝试过的那样?
我也可以摆脱“不存在必需的字符串参数'departure'”
沉没
答案 0 :(得分:0)
1。您的代码似乎没有包装所有html控件,因此将
<form action="/public/search" method="get">
...
在您的出发输入之前
2。将出发放到 select 控件(而不是选项)中,就像
<select name="departure">
到达时一样