【问题标题】:Parsing data Spring MVC解析数据 Spring MVC
【发布时间】:2018-11-29 23:01:51
【问题描述】:

控制器:

@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 EEST 出现意外错误(类型=内部服务器错误,状态=500)。 模板解析时出错(模板:“类路径资源[templates/result.html]”)

【问题讨论】:

    标签: java spring spring-mvc model-view-controller


    【解决方案1】:

    请尝试关闭段落标签。

    <p th:text="'city: ' + ${weather.city}" ></p>
    

    【讨论】:

    • 能否请您检查具体发生在哪一行错误
    • 它没有指定行,它在浏览器上显示本地主机上的消息:8080
    【解决方案2】:

    把post方法改成这样。

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

    在此处查找工作代码。 https://gitlab.com/supun/spring-boot-app/commit/b322644255e044ca5959460dd7d9f7a048a5f6d3

    【讨论】:

      猜你喜欢
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2012-01-12
      • 2015-05-28
      • 2015-03-11
      相关资源
      最近更新 更多