使用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}°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>