首页 > 解决方案 > Spring Boot 在 Post Mapping 中不返回模板

问题描述

我希望你们中的一些人对此有所了解。我对 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"


}

主页.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€&lt;/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€&lt;/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€&lt;/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€&lt;/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€&lt;/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€&lt;/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€&lt;/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€&lt;/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>

标签: springspring-bootthymeleaf

解决方案


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

                success: function(posdata) {

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

因此浏览器不会向您显示新的模板页面。您的页面在“postdata”响应中


推荐阅读