gpt4 book ai didi

java - 解析数据 Spring MVC

转载 作者:行者123 更新时间:2023-12-05 07:32:53 25 4
gpt4 key购买 nike

Controller :

@Controller
public class WeatherController {

@GetMapping("/weather")
public String weatherForm(Model model){
model.addAttribute("weather",new WeatherServiceImpl());
return "weather";
}

@PostMapping("/weather")
public String weatherSubmit(@ModelAttribute WeatherServiceImpl weather) {
return "result";
}
}

模板

<!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="@{/weather}" th:object="${weather}" method="post">
<p>City: <input type="text" th:field="*{city}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

结果模板

<!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>Result</h1>
<p th:text="'city: ' + ${weather.city}" />
<a href="/weather">Submit another message</a>
</body>
</html>

所以我试图使用表单从用户那里获取城市名称并将其解析为对象。它解析为对象。但是我尝试显示我从/weather 获得的城市名称,它不会转到/result 并且会显示一条错误消息白标错误页面此应用程序没有/error 的显式映射,因此您将其视为后备。

2018 年 6 月 20 日星期三 20:42:53 东部夏令时出现意外错误(类型=内部服务器错误,状态=500)。模板解析时发生错误(模板:“类路径资源[templates/result.html]”)

最佳答案

将发布方法改成这样。

@PostMapping("/weather")
public String weatherSubmit( WeatherServiceImpl weather,Model model) {
model.addAttribute("weather",weather);
return "result";
}

在这里找到工作代码。 https://gitlab.com/supun/spring-boot-app/commit/b322644255e044ca5959460dd7d9f7a048a5f6d3

关于java - 解析数据 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50954530/

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