eleaf和spring-boot-向模型添加属性正确的方法是什么?

k2arahey  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(305)

我正在尝试找到一个更好的方法来设置thymeleaf模板的值,因为我觉得我这样做是错误的。
假设我有一个具有两个Map的控制器:

@GetMapping("/")
    public String getSearchPage(Model model) {
        model.addAttribute("weatherRequest", new WeatherRequest());
        model.addAttribute("weatherResponse", new WeatherResponse());
        model.addAttribute("temperature_real");
        model.addAttribute("temperature_feels");
        model.addAttribute("humidity");
        model.addAttribute("pressure");
        return "index";
    }

@PostMapping("/getWeather")
    public String getWeatherForCity(@ModelAttribute("weatherRequest") WeatherRequest request, Model model) throws JsonProcessingException {

        WeatherResponse response = weatherService.getWeatherData(request.getZipCode());
        model.addAttribute("weatherResponse", new WeatherResponse());
        model.addAttribute("temperature_real", response.mainWeatherData.temperature);
        model.addAttribute("temperature_feels", response.mainWeatherData.temperatureFeels);
        model.addAttribute("humidity", response.mainWeatherData.humidity);
        model.addAttribute("pressure", response.mainWeatherData.pressure);
        return "index";
``` `@GetMapping("/")` 是我的主页, `@PostMapping("/getWeather")` 用于单击按钮时,以便为输入的邮政编码收集天气数据。
在我看来很奇怪,我添加了两次属性,但它是有效的。但是,当我试图更改模板,使窗体仅在 `mainWeatherData` 不为空。
相关的 `index.html` 你可以在下面找到)。
这部分是 `index.html` 改变了。
kuuvgm7e

kuuvgm7e1#

你的感情没有错。对于您尝试编写的示例,还有一些更有用的方法,我将向您展示我的简单和流行用法:

public class WeatherRequest {

    private String zipCode;

    public WeatherRequest() {
    }

    // getter/setter ..
}
public class WeatherResponse {

    private String temperatureReal;
    private String temperatureFeels;
    private String humidity;
    private String pressure;

    public WeatherResponse() {
    }

    // getter/setter ..
}
@Controller
public class WeatherController {

   @GetMapping("/")
   public String getSearchPage(Model model) {
      model.addAttribute("weatherRequest", new WeatherRequest());
      return "weather-search";
   }

   @PostMapping("/getWeather")
   public String getWeatherForCity(Model model, WeatherRequest weatherRequest) {
      model.addAttribute("weatherRequest", weatherRequest);
      model.addAttribute("weatherResponses", getWeatherResponse(weatherRequest.getZipCode()));
      return "weather-search";
   }

   private List<WeatherResponse> getWeatherResponses(String zipCode) {
      List<WeatherResponse> weatherResponses = new ArrayList<>();
      for (int i = 1; i < 3; i++) {
         WeatherResponse weatherResponse = new WeatherResponse();
         weatherResponse.setTemperatureReal("A" + i + ": " + zipCode);
         weatherResponse.setTemperatureFeels("B" + i + ": " + zipCode);
         weatherResponse.setHumidity("C" + i + ": " + zipCode);
         weatherResponse.setPressure("D" + i + ": " + zipCode);
         weatherResponses.add(weatherResponse);
      }
      return weatherResponses;
   }
}
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
    <form th:action="@{/getWeather}" method="post" th:object="${weatherRequest}">
        <label>Enter the postal code:</label>
        <input th:field="*{zipCode}" />
        <button type="submit">Check Weather</button>
    </form>
    <div th:if="${!weatherResponses.isEmpty()}">
        <div th:each="weatherResponse: ${weatherResponses}">
            <p>Temperature:<label th:text="${weatherResponse.temperatureReal}"></label></p>
            <p>Feels Like:<label th:text="${weatherResponse.temperatureFeels}"></label></p>
            <p>Humidity:<label th:text="${weatherResponse.humidity}"></label></p>
            <p>Pressure:<label th:text="${weatherResponse.pressure}"></label></p>
        </div>
    </div>
</body>
</html>

在thymeleaf中,迭代是通过使用 th:each 属性。
你可以走得更远。例如,当您单击“搜索”时,您可以 Ajax 请求并立即更新结果等。
如果你使用 Ajax 请求,请求 weatherRequestgetWeatherForCity() 方法将不需要。
这个链接示例将展示如何利用 Thymeleaf Fragments 要重用站点的某些公共部分,请执行以下操作:
处理thymeleaf中的片段
springmvc3.2thymeleafajax片段
springboot/thymeleaf:通过ajax的片段

相关问题