2

使用spring mvc,我想做一个网站,为您显示默认位置(基于ip)的天气,或者如果您输入地址,我希望它根据您的地址刷新。但是当我输入时它不会刷新为什么?我的课程中的所有数据都已更新 什么会导致我的问题以及如何解决?

我包括我的控制器 + jsp 文件,如果您需要任何其他文件,请告诉我我会更新这个问题。

演示网站:http://156.17.231.132:8080/

控制器:

@Controller
public class HomeController
{
    Weather weather;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String listUsers(ModelMap model, HttpServletRequest req) {

        Language.getInstance().setLanguage(LanguageManager.getLanguage(new Locale(System.getProperty("user.language"))));
        Location location = LocationManager.getLocation(req);
        weather = WeatherManager.getWeather(location);

        model.addAttribute("location", location);
        model.addAttribute("weather", weather);
        model.addAttribute("destination", new Destination());

        return "index";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("destination") Destination destination,BindingResult result) {
        destination = DestinationManager.getDestination(destination.getAddress());
        weather = WeatherManager.getWeather(destination);
        return "redirect:/";
    }
}

这是jsp文件:为什么我的if在这里不起作用?

<!doctype html>
<%@ page session="false" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="core" %>

<html>
<head>
    <meta charset="utf-8">
    <title>Weatherr</title>

    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="http://twitter.github.io/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
    <link href="http://twitter.github.io/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet">
</head>

<body>

<div class="container">
    <div class="row">
        <div class="span8 offset2">
            <%--<h2>Getting current weather data</h2>--%>
            <%--<h2>http://api.openweathermap.org/data/2.5/weather?lat=51.1&lon=17.0333&lang=en</h2>--%>
            <%--<h2>Getting forecast data every 3 hours</h2>--%>
            <%--<h2>http://api.openweathermap.org/data/2.5/forecast?lat=51.1&lon=17.0333&lang=en</h2>--%>
            <%--<h2>Getting daily forecast weather data - Seaching 10 days forecast by geographic coordinats </h2>--%>
            <%--<h2>http://api.openweathermap.org/data/2.5/forecast/daily?lat=51.1&lon=17.0333&cnt=10&mode=json&lang=en</h2>--%>

            <core:if test="${empty destination}">
                <h2>${location.city}, ${location.countryName}</h2>
            </core:if>

            <core:if test="${not empty destination}">
                <h2>${destination.address}</h2>
            </core:if>

            <h3>${weather.dt}</h3>

            <h3><img src="http://openweathermap.org/img/w/${weather.weatherData.icon}.png"
                     style="float:left; margin:5px 5px 5px 5px; border:0" alt="weather" width="20%" height="20%"/>

                ${weather.weatherData.description}</br>

                Temp: ${weather.main.temp}&deg;C</br>

                Wind speed: ${weather.wind.speed} mps, direction ${weather.wind.deg}  </br>

                Wind gust: ${weather.wind.gust} mps</br>

                Cloudiness: ${weather.clouds}% </br>

                Atmospheric pressure: ${weather.main.pressure}hPa </br>

                Humidity: ${weather.main.humidity}%</h3>

            <form:form method="post" action="search" commandName="destination" class="form-horizontal">
            <div class="control-group">
                <div class="controls">
                    <form:input path="address"/>
                    <input type="submit" value="Search" class="btn"/>
                    </form:form>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

</body>
</html>
4

1 回答 1

1

问题在于您的 POST 处理程序

@RequestMapping(value = "/search", method = RequestMethod.POST)
public String addUser(
    @ModelAttribute("destination") Destination destination,
    BindingResult result) {
    destination = DestinationManager.getDestination(destination.getAddress());
    weather = WeatherManager.getWeather(destination);
    return "redirect:/";
}

尽管您有一个@ModelAttributefor destinationwhich 将对象添加到当前请求的模型中,但您正在执行一个redirect. 重定向最终会发送 302(或 303)状态代码作为带有Location标头的响应。当浏览器收到此响应时,它会在标头指定的 url 向您的服务器发出新的 Http 请求Location。由于模型属性仅适用于一个请求,因此该destination属性不再存在于新请求的模型中。

无论如何,你仍然会用

model.addAttribute("destination", new Destination());

在您的 GET 处理程序中。

解决方案是将属性保留超过 1 个请求。换句话说,使用 flash 属性。Spring 提供了RedirectAttributes让你这样做的类。在这里查看它的实际应用。

如果您在 GET 中使用此解决方案,我建议destination您在覆盖之前先检查模型是否包含带有键的属性

if (!model.containsAttribute("destination"))
    model.addAttribute("destination", new Destination());
于 2013-09-04T15:52:37.087 回答