Java Spring mvc,为什么我的模型没有更新?如何修复?

Java Spring mvc,为什么我的模型没有更新?如何修复?,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,使用SpringMVC,我想做一个网站,显示您的默认位置(基于ip)的天气,或者如果您键入地址,我希望它根据您的地址刷新。 但是当我输入时,它不会刷新为什么? 我的类中的所有数据都会更新。什么会导致我的问题以及如何解决 我包括我的controller+jsp文件,如果您需要任何其他文件,请告诉我我将更新此问题 演示网站:http://156.17.231.132:8080/ 控制器: @Controller public class HomeController { Weather we

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

我包括我的controller+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>

天气预报员
${location.city},${location.countryName}
${destination.address}
${weather.dt}
${weather.weatherData.description}
温度:${weather.main.Temp}°;C
风速:${weather.Wind.speed}mps,方向${weather.Wind.deg}
阵风:${weather.Wind.gust}mps
云量:${weather.clouds}%
大气压力:${weather.main.pressure}hPa
湿度:${weather.main.湿度}%
问题在于您的邮件处理程序

@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:/";
}
尽管您有一个
@modeldattribute
用于
目的地
,将对象添加到当前请求的模型中,但您正在执行
重定向
。重定向最后发送一个302(或303)状态代码,作为带有
位置
头的响应。当浏览器收到此响应时,它会在
位置
标题指定的url处向服务器发出新的Http请求。由于模型属性仅对一个请求有效,因此新请求的模型中不再存在
目的地
属性

无论如何,您仍然会使用

model.addAttribute("destination", new Destination());
在你的GET处理器中

解决方案是将属性保留超过一个请求的时间。换句话说,使用flash属性。Spring提供了
RedirectAttributes
类来实现这一点。看到它在行动

如果使用此解决方案,在GET中,我建议首先检查模型是否包含具有key
destination
的属性,然后再覆盖它

if (!model.containsAttribute("destination"))
    model.addAttribute("destination", new Destination());

你能解释一下你提交帖子时的行为吗?我会写下我对它的理解,因为我可能错了,我是春天新来的。所以,当您键入某个城市时,我将其作为addres变量存储在我的类Destination中,然后在这个对象中使用GoogleAPI纬度和经度对该城市进行解析。然后我想在我的站点上更新我的天气,但这在我的站点上没有更新。因此,如果我理解正确,你建议我在GET(listUsers方法)中使用RedirectAttributes,但我如何检查它们是否存在?让你的POST方法处理程序接受一个
RedirectAttributes
参数。将flash属性添加到该对象并重定向。在执行GET处理程序之前,Spring会将这些属性添加到
模型中
,以便它们在方法中可用<代码>重定向attrs.addFlashAttribute(目标)或者我也做了天气预报?还是像这样<代码>重定向attrs.addAttribute(“id”,destination.getLatitude()).addFlashAttribute(“消息”,“找到位置!”)返回“重定向:/location/{id}”我尝试了第二个选项,因为我怀疑它是正确的,但我得到的异常如下:
http://pastebin.com/e8m7ysjd
@vardius您似乎需要更新版本的Spring才能使用它。