我有两个JSP页面让我们称之为1& 2.我在JSP页面1的javascript函数中通过AJAX查询将第1页的纬度和经度值传递给我的控制器类中的方法。控制器中的方法将使用纬度和经度来查询数据库并提供经过控制器的纬度和经度半径1公里范围内所有房屋的平均房价。我有一个调试打印语句,确认查询成功,因为它打印了Eclipse控制台中的平均房价。
如果您仍然和我在一起,我怎样才能从控制器方法中将此double
平均房价(housePriceAverage)
传递到JSP页码2上?出于某种原因,新的JSP页面(数字2)在调用时根本不会加载,但调试的工作原理是显示值正在传递给控制器并且查询有效吗?我真的很感激任何人的建议/提示!
这是我的Controller类中的方法示例。如果你想看到其他功能,我会乐意将它包括在内。谢谢!
@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) {
// This passes the lat & long into a method that will query the database
double housePriceAverage = parseHousePrice.ParseHousePrice(latitude, longitude);
// This print statement successfully prints the results fromt he query
System.out.println("The average house price for this area is: " + housePriceAverage);
model.addAttribute("houseprice", housePriceAverage);
// JSP Page I'm trying to pass the information above to
return "houseprice";
}
JSP第2页的代码,我想将数据(houseprice
)发送到控制器的
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
</script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>House Price</title>
<style>
I've excluded some CSS code here for conciseness
</style>
<script>
I've excluded Javascript code for a Navbar here for conciseness
</script>
</head>
<body>
<p style="display:none">${houseprice}</p>
<p style="display:none">${housepricelistsize}</p>
</body>
JSP 1中的Javascript函数,将纬度和经度数据发送到控制器
function sendDataToParseHousePrice(){
// Calculates the latitude and longitude of the user
$('.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
}, // parameters
datatype: 'json'
});
}
答案 0 :(得分:2)
由于您的方法使用@ResponseBody,因此文本houseprice将作为AJAX响应返回到第1页。它没有作为一个视图解决。如果你想坚持你的AJAX请求,你可以返回housePriceAverage。然后,当您在第1页上获得AJAX响应时,使用该值导航到第2页。在第2页中,使用@RequestParam将housePriceAverage作为参数提供
@RequestMapping(value = "/parseHousePrice", method={RequestMethod.POST, RequestMethod.GET})
public double parseHousePrice(@RequestBody HousePrice housePriceObject,
@RequestParam("latitude") double latitude,
@RequestParam("longitude") double longitude,
Model model) {
// This passes the lat & long into a method that will query the database
double housePriceAverage = parseHousePrice.ParseHousePrice(latitude, longitude);
// This print statement successfully prints the results fromt he query
System.out.println("The average house price for this area is: " + housePriceAverage);
return housePriceAverage;
}
如果可以放弃AJAX,那么对你的Controller进行常规POST并使用视图分辨率导航到第2页。你可以通过删除@ResponseBody来实现这个目的
@RequestMapping(value = "/parseHousePrice", method={RequestMethod.POST, RequestMethod.GET})
public String parseHousePrice(@RequestBody HousePrice housePriceObject,
@RequestParam("latitude") double latitude,
@RequestParam("longitude") double longitude,
Model model) {
// This passes the lat & long into a method that will query the database
double housePriceAverage = parseHousePrice.ParseHousePrice(latitude, longitude);
// This print statement successfully prints the results fromt he query
System.out.println("The average house price for this area is: " + housePriceAverage);
model.addAttribute("houseprice", housePriceAverage);
// JSP Page I'm trying to pass the information above to
return "houseprice";
}