Spring Boot不会在Post映射中返回模板

Spring Boot不会在Post映射中返回模板,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我希望你们中的一些人对此有所了解。我对Spring还是新手,但到目前为止,我可以自己解决所有问题。我也在使用Thymeleaf作为模板。 在控制器中,@GetMapping函数按预期工作,但@PostMapping函数不返回我在返回参数中指定的模板,因此浏览器显示错误的页面。 @PostMapping函数由html脚本中的ajax语句触发 我在服务器端和客户端都没有收到错误或警告。所以我不知道是哪里出了错 希望你们中的一些人能发现错误。无论如何,先谢谢你 控制器: @Controller pub

我希望你们中的一些人对此有所了解。我对Spring还是新手,但到目前为止,我可以自己解决所有问题。我也在使用Thymeleaf作为模板。 在控制器中,@GetMapping函数按预期工作,但@PostMapping函数不返回我在返回参数中指定的模板,因此浏览器显示错误的页面。 @PostMapping函数由html脚本中的ajax语句触发

我在服务器端和客户端都没有收到错误或警告。所以我不知道是哪里出了错

希望你们中的一些人能发现错误。无论如何,先谢谢你

控制器:

@Controller
public class SpringController {


@RequestMapping(value="/tank", method = RequestMethod.GET)
public String loadData(Model model){

    Tankstelle[] data = TankerAPI.getTankData(lat, lng, 5, sort, type);

    model.addAllAttributes(convertdata("name", data));
    model.addAllAttributes(convertdata("dist", data));
    model.addAllAttributes(convertdata("price", data));

    return "home";
}



@RequestMapping(value = "/tank", method = RequestMethod.POST)
public String getChangedData(@RequestBody JSONObject incomingjson) {

    lat = Float.parseFloat(incomingjson.get("lat").toString());
    lng = Float.parseFloat(incomingjson.get("lng").toString());
    BigDecimal biglat = new BigDecimal(lat).setScale(3, RoundingMode.HALF_UP);
    BigDecimal biglng = new BigDecimal(lng).setScale(3, RoundingMode.HALF_UP);
    lat = biglat.floatValue();
    lng = biglng.floatValue();

    sort = "price"; //for seeing a difference

    System.out.println(incomingjson.get("lat")+" "+incomingjson.get("lng"));

    return "test";   //here it should return the template "test" but it return "home"


}
home.html:

    <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8"/>
    <title>guenstigertanken.de</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <style>...
    </style>


</head>
<body onload="getLocation()">
    <div class="ueberschriftbox"></div>
   <h2 class="header">guenstigertanken.de</h2>
    <h2 class="subheader">Wir finden die günstigsten Spritpreise für Sie</h2>
    <div class="backrounding">    

        <div class="steuerelementebox"></div>
        <div class="steuerelementespritsorteueberschrift">
                <h2>Spritsorte</h2>
            <label class="container1">Super E5
              <input type="radio" id="E5" name="radio1" onclick="autoSubmit()">
              <span class="checkmark1"></span>
            </label>
            <label class="container1">Super E10
              <input type="radio" id="E10" name="radio1" onclick="autoSubmit()">
              <span class="checkmark1"></span>
            </label>
            <label class="container1">Diesel
              <input type="radio" id="Diesel" name="radio1" onclick="autoSubmit()">
              <span class="checkmark1"></span>
            </label> 
        </div> 

            <div class="steuerelementesortierennachueberschrift">
                <h2>Sortieren nach</h2>
                <label class="container2">Preis aufsteigend
                  <input type="radio" checked="checked" name="radio2" onclick="autoSubmit()">
                  <span class="checkmark2"></span>
                </label>
                <label class="container2">Preis absteigend
                  <input type="radio" name="radio2" onclick="autoSubmit()">
                  <span class="checkmark2"></span>
                </label>
                <label class="container2">Distanz aufsteigend
                  <input type="radio" name="radio2" onclick="autoSubmit()">
                  <span class="checkmark2"></span>
                </label> <label class="container2">Distanz absteigend
                  <input type="radio" name="radio2" onclick="autoSubmit()">
                  <span class="checkmark2"></span>
                </label>
                 <label class="container2" id="alph">Name alphabetisch
                  <input type="radio" name="radio2" onclick="autoSubmit()">
                  <span class="checkmark2"></span>
                </label>
            </div>       
        <div class="buttonsbox"></div>
        <div class="tabellenbox"></div>
        <div class="ueberuns">
            <p>Über uns</p>  
        </div>
        <div class="agb">
            <p>AGB</p>  
        </div>
        <div class="datenschutz">
            <p>Datenschutz</p>  
        </div>
        <div class="impressum">
            <p>Impressum</p>  
        </div>
        <div class="left show">
            <table id="datatable">
                <thead>
                <tr>    <th>Name</th>   <th>Entfernung</th>     <th>Preis</th>  <th>Favorit</th> </tr>
                </thead>
                <tbody>
                <tr> <td th:text="${name1}">AGIP</td> <td th:text="${dist1}">7km</td> <td th:text="${price1}">1,20€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name2}">Aral</td> <td th:text="${dist2}">12km</td> <td th:text="${price2}">1,23€</td> <td><input type="checkbox"></td>  </tr>
                <tr> <td th:text="${name3}">Esso</td> <td th:text="${dist3}">2km</td> <td th:text="${price3}">1,25€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name4}">Esso</td> <td th:text="${dist4}">10km</td> <td th:text="${price4}">1,25€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name5}">Esso</td> <td th:text="${dist5}">5km</td> <td th:text="${price5}">1,25€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name6}">BP</td> <td th:text="${dist6}">13km</td> <td th:text="${price6}">1,35€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name7}">BP</td> <td th:text="${dist7}">13km</td> <td th:text="${price7}">1,35€</td> <td><input type="checkbox"></td> </tr>
                <tr> <td th:text="${name8}">BP</td> <td th:text="${dist8}">13km</td> <td th:text="${price8}">1,35€</td> <td><input type="checkbox"></td> </tr>
                </tbody>
            </table>
        </div> 
  </div>
   <a class="cssbuttonfavoriten" href="test.html">Favoriten></a>
   <a class="cssbuttonleaderboard" href="test.html">Leaderboard></a>
   <a class="cssbuttonstatistiken" href="test.html">Statistiken></a>



    <script>

        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {

            }

        }

        function showPosition(position) {

            var posdata = '{"lat":"'+position.coords.latitude+'","lng":"'+position.coords.longitude+'","typeselect":"'+type+'"}';

            $.ajax({
                type: 'post',
                url: '/tank',
                data: JSON.stringify(posdata),
                contentType: "application/json; charset=utf-8",
                traditional: true,
                success: function(posdata) {

                    //alert("ajax success: refresh page to sort after price[prob current setting]")
                },
                error: function(){
                    alert("ajax error")
                }
            });
        }


    </script>

</body>
</html>

格恩斯蒂格坦肯德酒店
...
格恩斯蒂格坦肯德酒店
你能不能找到一份工作
斯普里斯特
超级E5
超级E10
柴油机
Sortieren nach
奥夫斯坦根德酒店
Preis Absegned
奥夫斯坦区
迪斯坦兹酒店
姓名字母表
Über uns

AGB

Datenschutz

预付款

名称Entfernung Preis Favorit AGIP 7公里1.20欧元 咸海12公里1.23欧元 埃索2公里1.25欧元 埃索10公里1.25欧元 埃索5公里1.25欧元 英国石油公司13公里1.35欧元 英国石油公司13公里1.35欧元 英国石油公司13公里1.35欧元 函数getLocation(){ if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(showPosition); }否则{ } } 功能显示位置(位置){ var posdata='{“lat”:“+position.coords.latitude+”,“lng”:“+position.coords.longitude+”,“typeselect”:“+type+”}”; $.ajax({ 键入:“post”, url:“/tank”, 数据:JSON.stringify(posdata), contentType:“应用程序/json;字符集=utf-8”, 传统的:是的, 成功:函数(posdata){ //警报(“ajax成功:刷新页面以在价格[prob当前设置]后排序”) }, 错误:函数(){ 警报(“ajax错误”) } }); }
当您通过ajax发出任何请求时,您只需在回调函数中收到相应的响应:

                success: function(posdata) {

                //alert("ajax success: refresh page to sort after price[prob current setting]")
            },

所以浏览器不会向您显示新的模板页面。您的页面处于“postdata”响应中

当您通过ajax发出任何请求时,您只需在回调函数中收到相应的响应:

                success: function(posdata) {

                //alert("ajax success: refresh page to sort after price[prob current setting]")
            },

所以浏览器不会向您显示新的模板页面。您的页面处于“postdata”响应状态

谢谢,我是javascript的绝对初学者,如何更改这一点,这不是posdata响应?当您发送ajax请求时,function()或function(response)不起作用,这意味着您的代码处理任何响应。即使使用不带参数的function(),也不意味着浏览器将更新页面。我不确定是否有任何方法可以强制浏览器处理该响应并呈现网页谢谢,我是javascript的绝对初学者,我如何改变这一点,这不是posdata响应?当您发送ajax请求时,函数()或函数(响应)不起作用,这意味着您的代码可以处理任何响应。即使使用不带参数的function(),也不意味着浏览器将更新页面。我不确定是否有任何方法可以强制浏览器处理该响应并呈现网页