[英]How do I pass a value from a controller to a JSP page in Spring?
I'm trying to pass data from a Controller to a JSP page but I'm running into a few issues. 我正在尝试将数据从Controller传递到JSP页面,但是遇到了一些问题。 There's a method in the Controller that will query a database of house price information based on a latitude and longitude value provided by a user.
Controller中有一种方法,可以根据用户提供的经度和纬度值查询房价信息数据库。 A debug shows that the database is queried correctly as it prints a
houseprice
value and the number of houses
in the results to Eclipe's console but I can't get the JSP page to load and therefore get it to print to the JSP page. 调试显示在打印数据库
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";
}
The data is sent via an AJAX request in a Javascript function on one JSP and I'm trying to open a new JSP page with the information from the controller 数据是通过一个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'
});
}
And I call it from a link in a navbar like so: 我从导航栏中的链接中这样称呼它:
<li><a href= "javascript:parseHousePrice();" >House Price</a></li>
In the JSP page I've tried a few ways to collect the information calculated above and display it. 在JSP页面中,我尝试了几种方法来收集上面计算的信息并显示出来。 One way was use thymleaf but this won't print
一种方法是使用百里香,但这不会打印
<h4>Average House Price <text th:text="${houseprice}" /> </h4>
<h4>Number of Houses the average is caluclated by: <text th:text="${housepricelistsize}" /> </h4>
Another way was to drop the thymeleaf and call it like this: 另一种方法是放下胸腺叶并这样称呼它:
<h1>${houseprice} </h1>
<h1>${housepricelistsize} </h1>
However, there is no luck. 但是,没有运气。 I can get the information successfully passed into the controller and printed into the console, but passing it from the controller to the JSP page is where I'm having trouble.
我可以将信息成功传递到控制器并打印到控制台,但是我遇到了麻烦,将其从控制器传递到JSP页面。 Any help would be appreciated!
任何帮助,将不胜感激!
As far as I understand, you are trying to update your jsp page with house price and size when navbar is clicked. 据我了解,当您单击导航栏时,您正在尝试使用房价和大小更新jsp页面。 I am trying to answer this based on what I understand.
我试图根据我的理解回答这个问题。
In your jsp I would put something like, 在您的jsp中,我会输入以下内容:
<h1 id ="housepriceID"></h1>
<h1 id ="housepricesize"> </h1>
Now on your jQuery, 现在在您的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);
});
This should solve your problem. 这应该可以解决您的问题。
如果我没看错,代码model.addAttribute
应该是model.setAttribute
,因为您要将从数据库查询的数据添加到会话中,因此必须改用.setAttribute
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.