Java 如何在Spring中将数据从控制器发送到JSP页面?
我有两个JSP页面,我们把它们称为1和2。我通过JSP第1页javascript函数中的AJAX查询将纬度和经度值从第1页传递给控制器类中的方法。控制器中的方法将使用纬度和经度查询数据库,并提供传入控制器的纬度和经度半径为1km的所有房屋的平均房价。我有一个debug print语句,它在Eclipse控制台中打印平均房价时确认查询成功 如果您仍然与我在一起,那么我如何将此Java 如何在Spring中将数据从控制器发送到JSP页面?,java,spring,jsp,Java,Spring,Jsp,我有两个JSP页面,我们把它们称为1和2。我通过JSP第1页javascript函数中的AJAX查询将纬度和经度值从第1页传递给控制器类中的方法。控制器中的方法将使用纬度和经度查询数据库,并提供传入控制器的纬度和经度半径为1km的所有房屋的平均房价。我有一个debug print语句,它在Eclipse控制台中打印平均房价时确认查询成功 如果您仍然与我在一起,那么我如何将此双平均房价值(房价平均值)从控制器方法传递到JSP第2页上显示?出于某种原因,新的JSP页面(编号2)在调用时不会加载,但是
双
平均房价值(房价平均值)
从控制器方法传递到JSP第2页上显示?出于某种原因,新的JSP页面(编号2)在调用时不会加载,但是调试可以显示值正在传递给控制器,查询可以工作吗?我非常感谢任何人的建议/提示
下面是我的控制器类中的方法示例。如果你想看到其他功能,我很乐意将其包括在内。谢谢大家!
@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>
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'
});
}
由于方法上有@ResponseBody,所以文本houseprice作为AJAX响应返回到第1页。它不是作为视图解析的。如果您想坚持AJAX请求,可以返回housePriceAverage。然后,当您在第1页获得AJAX响应时,使用该值导航到第2页。在第2页中,使用@RequestParam获取作为参数提供的房价平均值
@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,那么向控制器发送一篇常规帖子,并使用视图分辨率导航到第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";
}
你可以添加你的JSP代码,这将澄清问题清楚地添加到JSP代码。谢谢 你真的在这里检查,因为你所做的是你没有显示你的段落。你能在${houseprice}段之外的任何地方检查这个值吗?谢谢你的回复。我已经包含了来自JSP第1页的AJAX查询,我如何
使用该值导航到第2页。然后在第2页中,使用@RequestParam获取作为参数提供的房价平均值,如您所说。很抱歉,我只是有点不确定我会怎么做,因为我对这个概念很陌生。