使用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文件: 也为什么我的在这里不起作用?
<!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}°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>
答案 0 :(得分: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:/";
}
虽然@ModelAttribute
destination
将{I}添加到当前请求的模型中,但您正在执行redirect
。重定向最终会发送302(或303)状态代码作为带有Location
标头的响应。当浏览器收到此响应时,它会在Location
标头指定的URL处向服务器发出新的Http请求。由于模型属性仅针对一个请求生效,因此新请求的模型中不再存在destination
属性。
无论如何,你仍然会用
覆盖它model.addAttribute("destination", new Destination());
在您的GET处理程序中。
此解决方案是将属性保留为超过1个请求。换句话说,使用flash属性。 Spring提供了RedirectAttributes
类来让你这样做。在行动here中查看。
如果你使用这个解决方案,在你的GET中,我建议你首先检查模型是否包含一个带有键destination
的属性,然后再覆盖它
if (!model.containsAttribute("destination"))
model.addAttribute("destination", new Destination());