java - Spring mvc, why my model is not updating? how to fix ? -


using spring mvc want website showing weather default location (based on ip) or if type in address want refresh based on ur address. when type in doesnt refresh why ? data updated in classes can cause problem , how fix ?

i include controller + jsp file if need other please tell update question.

demo website: http://156.17.231.132:8080/

controller:

@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:/";     } } 

and here jsp file: why if doesnt work here ?

<!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 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> 

the issue post handler

@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:/"; } 

although have @modelattribute destination adds object model current request, performing redirect. redirect ends sending 302 (or 303) status code response location header. when browser receives response, makes new http request server @ url specified location header. since model attributes live 1 request, destination attribute no longer exists in model of new request.

regardless, still overwrite with

model.addattribute("destination", new destination()); 

in handler.

the solution persist attributes longer 1 request. in other words, use flash attributes. spring provides redirectattributes class let that. see in action here.

if use solution, in get, suggest first checking if model contains attribute key destination before overwrite

if (!model.containsattribute("destination"))     model.addattribute("destination", new destination()); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -