简体   繁体   English

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

[英]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! 任何帮助,将不胜感激!

Screenshot of application 应用程序的屏幕截图 屏幕截图

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.

相关问题 如何将值从Spring Controller传递到jsp页面?(传递的值将用于初始化jsp页面中的javascript变量 - How to pass values from Spring Controller to jsp page?( the passed values is to be used to intialise javascript variables in jsp page 如何通过 spring 控制器将 jsp 页面作为弹出窗口打开? - How do I open a jsp page as a popup via a spring controller? 如何将 jsp 页面中的值传递到 javascript 页面? - How can I pass a value from a jsp page to a javascript page? 如何将文本字段值从jsp传递到spring控制器类,反之亦然 - How to pass text field value from a jsp to spring controller class and vice versa 如何将列表从控制器传递到jsp并在Spring MVC中的javascript中对其进行迭代 - how to pass a list from controller to jsp and iterate it in javascript in spring mvc 如何在Spring MVC中将图像从jsp传递到控制器 - How to pass Image from jsp to controller in Spring MVC 如何将一个JSP的输入类型值或ID传递给另一个JSP页面 - How can I pass input type value or id from one jsp to another jsp page 将值从.jsp传递到新页面.jsp - Pass Value from .jsp to new page .jsp 我可以将数据从Spring引导控制器传递到html页面,但是如何使用Java Script访问数据? - I am able to pass the data from Spring boot controller to html page, but how do I access the data in Java Script? 如何将json数据从jsp页面发送到弹簧控制器 - How can I send json data from my jsp page to a spring rest controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM