简体   繁体   中英

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";
    }

Either use onsubmit and have getLocation return false, then submit form manually in showPosition , or use jQuery/YUI/something to attach getLocation as a non-destructive event handler.

EDIT: Perhaps something like this: http://jsfiddle.net/mz6zN/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM