简体   繁体   中英

How do I send data from a Controller to a JSP page in Spring?

I have two JSP pages let's just called them 1 & 2. I'm passing a latitude and longitude value from page 1 to a method in my controller class via an AJAX query in a javascript function on JSP page 1. The method in the controller will use the latitude and longitude to query a database and provide an average house price of all houses in a 1km radius of the latitude and longitude passed into the controller. I have a debug print statement that confirms that the query is successful as it prints the average house price in the Eclipse console.

If you're still with me, how can I then pass this double average house price value (housePriceAverage) from the controller method to display on JSP page number 2? For some reason, the new JSP page (number 2) simply will not load when called, however the debug works to show the values are being passed to the controller and the query works? I would really appreciate any advice/tips anyone would have!

Here's an example of the method inside my Controller class. If you wanna see the other functionality I'll be happy to include it. Thank You!

 @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"; 
}

Code for JSP page 2 where I want to send the data ( houseprice ) to, from the controller

<%@ 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>

Javascript function in JSP 1 that sends the latitude and longitude data to the controller

    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'
    });

    }

Because of @ResponseBody on your method, the text houseprice is returned to page 1 as an AJAX response. It is not resolved as a view. If you want to stick to your AJAX request you can return the housePriceAverage instead. Then when you get the AJAX response on page 1 use the value to navigate to page 2. In page 2 use @RequestParam to get the housePriceAverage supplied as a parameter

@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;
}

If its possible to forgo AJAX then do a conventional POST to your Controller and use view resolution to navigate to page 2. You achieve this by removing @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"; 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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