Thursday, 15 August 2013

JS and Spring troubles

JS and Spring troubles

I made a little script which returns longitude and latidue to value of
input form. In HTML:
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value =
position.coords.longitude;
}
</script>
<input type="text" id="latitude" />
<input type="text" id="longitude" />
It works. But I want to make it works in Spring. I was trying to do this:
<p id="demo">Click the button to get your coordinates:</p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value =
position.coords.longitude;
}
</script>
<form:form method="POST" commandName="geolocation">
<form:input path="latitude" id="latitude" type="text" />
<form:input path="longitude" id="longitude" type="text" />
<input id="bigbutton" type="submit" onclick="getLocation()"
value="Confirm" />
</form:form>
But it returns me zeros. What's wrong? This is my form class:
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
Controller:
@RequestMapping(value = "/about", method = RequestMethod.GET)
public String getAboutPage(Map<String, Object> map,
@ModelAttribute("geolocation") GeoLocation geolocation,
@ModelAttribute("search") SearchForm query, BindingResult
result) {
map.put("news", new News());
map.put("newsList", newsService.getAboutPage());
map.put("temp", TEMP);
return "about";
}
@RequestMapping(value = "/about", method = RequestMethod.POST)
public String getAboutPageProcess(Map<String, Object> map,
@ModelAttribute("geolocation") GeoLocation geolocation,
@ModelAttribute("search") SearchForm query, BindingResult
result) {
System.out.println(geolocation.getLatitude());
System.out.println(geolocation.getLongitude());
map.put("news", new News());
map.put("newsList", newsService.getAboutPage());
map.put("temp", TEMP);
return "about";
}

No comments:

Post a Comment