繁体   English   中英

如何在Spring中将值从控制器传递到JSP页面?

[英]How do I pass a value from a controller to a JSP page in Spring?

我正在尝试将数据从Controller传递到JSP页面,但是遇到了一些问题。 Controller中有一种方法,可以根据用户提供的经度和纬度值查询房价信息数据库。 调试显示在打印数据库houseprice值和结果中的houses数到Eclipe的控制台时,数据库查询正确,但是我无法加载JSP页面,因此无法将其打印到JSP页面。

 @RequestMapping(value = "/parseHousePrice", method={RequestMethod.POST, RequestMethod.GET})
 public @ResponseBody String parseHousePrice(@RequestBody HousePrice housePriceObject, 
                                             @RequestParam("latitude") double latitude,
                                             @RequestParam("longitude") double longitude, 
                                             Model model) {

  double housePriceAverage = parseHousePrice.ParseHousePrice(latitude, longitude);

  // This passes the lat & long into a method that will query the database
  List<Double> housePriceList = parseHousePrice.getList();
  // This will return the number of houses that were queried
  int housePriceListSize = housePriceList.size();

  // This print statement successfully prints the results fromt he query
  System.out.println("The average house price for this area is: " + housePriceAverage + " based on " + housePriceListSize + " property prices in this area");

  model.addAttribute("houseprice", housePriceAverage);
  model.addAttribute("housepricelistsize", housePriceListSize);

  // JSP Page I'm trying to pass the information above to
  return "houseprice"; 
}

数据是通过一个JSP上的Javascript函数中的AJAX请求发送的,而我正尝试使用来自控制器的信息打开一个新的JSP页面

   function parseHousePrice(){

       // Calculates users latitude and longitude
       $('.search_latitude').val(marker.getPosition().lat());
       var Lat = marker.getPosition().lat();
       console.log(Lat);

       $('.search_longitude').val(marker.getPosition().lng());
       var Long = marker.getPosition().lng();
       console.log(Long);

       $.ajax({
                type: "POST",
                url: "/parseHousePrice",
                data: { latitude: Lat, 
                        longitude: Long,  
                      }, 
                datatype: 'json'
       });

    }

我从导航栏中的链接中这样称呼它:

 <li><a href= "javascript:parseHousePrice();" >House Price</a></li>

在JSP页面中,我尝试了几种方法来收集上面计算的信息并显示出来。 一种方法是使用百里香,但这不会打印

 <h4>Average House Price <text th:text="${houseprice}" /> </h4>
 <h4>Number of Houses the average is caluclated by: <text th:text="${housepricelistsize}" /> </h4>

另一种方法是放下胸腺叶并这样称呼它:

 <h1>${houseprice} </h1>
 <h1>${housepricelistsize} </h1> 

但是,没有运气。 我可以将信息成功传递到控制器并打印到控制台,但是我遇到了麻烦,将其从控制器传递到JSP页面。 任何帮助,将不胜感激!

应用程序的屏幕截图 屏幕截图

据我了解,当您单击导航栏时,您正在尝试使用房价和大小更新jsp页面。 我试图根据我的理解回答这个问题。

在您的jsp中,我会输入以下内容:

<h1 id ="housepriceID"></h1>
 <h1 id ="housepricesize"> </h1>

现在在您的jQuery上,

$.ajax({
                type: "POST",
                url: "/parseHousePrice",
                data: { latitude: Lat, 
                        longitude: Long,  
                      }, 
                datatype: 'json'
       })
.done(function( data, textStatus, jqxhr ) {    
    $("#housepriceID").val(data.houseprice);
    $("#housepricesize").val(data.housepricelistsize);

            });

这应该可以解决您的问题。

如果我没看错,代码model.addAttribute应该是model.setAttribute ,因为您要将从数据库查询的数据添加到会话中,因此必须改用.setAttribute

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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