【问题标题】:JS and Spring troublesJS和Spring的烦恼
【发布时间】:2013-08-15 22:00:16
【问题描述】:

我制作了一个小脚本,它将经度和纬度返回到输入表单的值。在 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" />

它有效。但我想让它在春天工作。 我试图这样做:

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

但它返回零。怎么了? 这是我的表单类:

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

控制器:

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

【问题讨论】:

    标签: java javascript html spring


    【解决方案1】:

    要么使用onsubmit 并让getLocation 返回false,然后在showPosition 中手动提交表单,或者使用jQuery/YUI/something 附加getLocation 作为非破坏性事件处理程序。

    编辑:也许是这样的:http://jsfiddle.net/mz6zN/

    【讨论】:

    • &lt;input id="bigbutton" type="submit" onsubmit="getLocation()" value="Confirm" /&gt; 下一步是什么?
    • 查看我发布的 jsfiddle 以获取示例代码。请注意,实际上在 jsfiddle 中提交表单不起作用(并且不应该这样做,请参阅 stackoverflow.com/questions/11528621/… ),但它应该在您的页面上工作。
    • &lt;p id="demo"&gt;Click the button to get your coordinates:&lt;/p&gt; &lt;form:form method="POST" id="geolocationForm" commandName="geolocation"&gt; &lt;form:input path="latitude" id="latitude" /&gt; &lt;form:input path="longitude" id="longitude" /&gt; &lt;button id="bigbutton" onclick="return getLocation()"&gt;Confirm&lt;/button&gt; &lt;/form:form&gt;
    • 您的控制器方法也存在问题 - 它需要两个 @ModelAttribute 参数,但我认为 Spring MVC 不允许将其用于单个表单 POST。 SearchForm 在 JSP 中是如何建模/绑定的?每个 POST 只能提交一个表单,因此您可能必须将 GeoLocationSearchForm 组合成一个命令对象。看到这个旧线程:forum.springsource.org/…
    • 搜索表单正在使用 GET
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2016-07-16
    相关资源
    最近更新 更多