my controller:
public String showWeather(Model model) {
model.addAttribute("weather", weatherService.listCities());
return "weather";
}
my weather.jsp
<select class="data">
<c:forEach items="${weather}" var="city">
<option>${city.name}</option>
</c:forEach>
</select>
How can I make that everytime I highlight/select something from the dropdown menu, new info would be displayd next to it from database according to the City?
First of all, must I send all data from database to my JSP with controller at first, or can data be transfered from database to JSP meanwhile I highlight/select items on the list(so when I select city “A”, then the query will get all information about city “A” and I can use the info)?
here are my tables:
CITIES(id serial, name varchar(40))
WEATHER(id serial, city_id int, temp int, data date)
So basically I have list of CITIES in my dropdown menu, and when city is selected, then the WEATHER with that city_id will be queried from database in theory.
If the live updating is not possible, how should I do it otherways?
Feel free to ask questions or give suggestions.
I think you should use AJAX in this case
use JQUERY on change event whick will send your cityId to SpringMVC controller, as the response your controller should return wheather, and then update your GUI component with this result:
then main difference is that your controller should return anything but the view. You can do it using
@ResponseBodyannotation:for example this controller will return you temperature as String when you request
it for city with id=222
/getTemperature/222
in this case that code behaves exactly like Servlet where you write your result in
HttpServletResponse.Hope it will help you.