gpt4 book ai didi

java - 如何将数据从表单发送到后端? [Java]

转载 作者:行者123 更新时间:2023-12-02 09:52:13 24 4
gpt4 key购买 nike

如何从表单 View 向后端发送数据?我想使用收集到的数据创建 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;
}
}

负责从 View 收集数据的类

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 构建器类负责构建请求。

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() +
"&currency=" + getCurrency() +
"&return_date=" + getDateTo() +
"&token=" + key;
return connectUrlString ;
}



}

我尝试通过多种方式解决我的问题。不幸的是没有用,这就是我决定创建一个线程的原因。我找不到类似的问题。我可能正在寻找一个坏的。但是,我不知道如何谷歌

我得到空响应:

http://api.travelpayouts.com/v1/prices/cheap?origin=null&destination=null&depart_date=null&currency=null&return_date=null&token=47c5ebee552ce27c902e7521b6ef3858

最佳答案

上面的代码将不起作用,因为您正在创建新的 UrlBuilder 对象,该对象不会有任何值,而不是从 Controller 传递 flightDTO 对象。

Controller

@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(flightDTO); //passing flightDTO object received from form
}

Connect

public String connect(FlightDTO flightDTO) { //Added new parameter to recevice flightDTO
String output = null;

try {
UrlBuilder urlBuilder = new UrlBuilder();
urlBuilder.setOrigin(flightDTO.getOrigin());
urlBuilder.setDestination(flightDTO.getDestination());
urlBuilder.setDateFrom(flightDTO.getDateFrom());
urlBuilder.setDateTo(flightDTO.getDateTo());
......//Set other required values
urlBuilder.ulr();

System.out.println("URL String : " + urlBuilder.ulr());
......//other code
}
}

关于java - 如何将数据从表单发送到后端? [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com