【发布时间】:2019-10-07 20:34:58
【问题描述】:
如何将数据从表单视图发送到后端?我想使用收集到的数据来创建 JSON 请求。
@Controller
public class ControllerClass {
Connect connect = new Connect();
@RequestMapping(value = "/Search", method = RequestMethod.GET)
public ModelAndView showForm() {
return new ModelAndView("Forms", "FlightDTO", new FlightDTO());
}
@RequestMapping(value = "/connect", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute("FlightDTO") FlightDTO flightDTO,
BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "error.jsp";
}
return connect.connect();
}
}
查看类收集数据。
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/connect}" th:object="${FlightDTO}" method="post">
<p>Orgin: <input type="text" th:field="*{Origin}" /></p>
<p>Departure: <input type="text" th:field="*{Departure}" /></p>
<p>DateFrom: <input type="text" th:field="*{DateFrom}" /></p>
<p>DateTo: <input type="text" th:field="*{DateTo}" /></p>
<p>Currency: <input type="text" th:field="*{Currency}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
负责消费数据 JSON 的类。
public class Connect {
public String connect() {
String output = null;
try {
UrlBuilder urlBuilder = new UrlBuilder();
urlBuilder.ulr();
System.out.println("URL String : " + urlBuilder.ulr());
URL url = new URL(urlBuilder.ulr());
HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode());
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
output = response.toString();
} catch (Exception e) {
System.out.println("Exception Flight:- " + e);
}
return output;
}
}
负责从视图中收集数据的类
public class FlightDTO {
private String dateFrom;
private String dateTo;
@Size(min = 2, max = 10)
private String origin;
@Size(min = 2, max = 10)
private String departure;
@Size(min = 2, max = 4)
private String currency;
我的 URL builder 类负责构建请求。
public class UrlBuilder extends FlightDTO {
private String key = "47c5ebee552ce27c902e7521b6ef3858";
public String ulr( ) {
String connectUrlString =
"http://api.travelpayouts.com/v1/prices/cheap?origin="
+ getOrigin() + "&destination=" + getDeparture() +
"&depart_date=" + getDateFrom() +
"¤cy=" + getCurrency() +
"&return_date=" + getDateTo() +
"&token=" + key;
return connectUrlString ;
}
}
我尝试通过多种方式解决我的问题。不幸的是无济于事,这就是我决定创建一个线程的原因。我找不到类似的问题。我可能正在寻找一个坏的。但是,我不知道如何谷歌
我得到空响应:
http://api.travelpayouts.com/v1/prices/cheap?origin=null&destination=null&depart_date=null¤cy=null&return_date=null&token=47c5ebee552ce27c902e7521b6ef3858
【问题讨论】:
标签: java json spring model-view-controller