我试图将两个值从我的控制器类中的方法传递到JSP页面,控制器中的功能似乎可以工作,但JSP页面不会加载。这两个值是average house price
和表示从数据库查询返回的number of houses
的整数值。我从不同的JSP页面中获取纬度和经度值,它们确定从数据库返回的值。 (我不想将average house price
和number of houses
值传递回原来来自纬度和经度的相同JSP页面,而是新页面。
在我的控制器类中,我有一个调试,可以在Eclipse控制台中成功打印数据库查询的结果,所以我知道查询有效,我还包含了一个print语句来查看{{1} }包含一个值,它打印成功。所以看起来这些值是正确的并准备好发送到新的JSP页面但是我尝试加载的页面只是没有加载而且我已经尝试过几种不同的方法来解决这个问题,但没有办法(我将在下面介绍这些方法)。
目前我尝试使用session
将值从控制器传递到新JSP页面,如下所示:
HttpSession
在我的新JSP页面中,我尝试将上面的值传递到以下 @RequestMapping(value = "/parseHousePrice", method = RequestMethod.GET)
public String parseHousePrice(@RequestParam("latitude") double latitude,
@RequestParam("longitude") double longitude,
HttpSession session) {
// This passes the lat & long into a method that will query the database
// and store the result into the housePrice double varaible
double housePrice = parseHousePrice.ParseHousePrice(latitude, longitude);
// This gets the number of houses returned from the query
// and stores it into the number_of_houses integer value
List<Double> housePriceList = parseHousePrice.getList();
int number_of_houses = housePriceList.size();
// Debug to show the query works successfullt
System.out.println("The average house price for this area is: " + housePrice + " based on " + number_of_houses + " property prices in this area");
// Assigning the values to session attributes
session.setAttribute("houseprice", housePrice );
session.setAttribute("housepricelistsize", number_of_houses);
// Debug that shows the session currently holds a value
System.out.println(session.getServletContext());
// New JSP page I want to pass the values to
return "houseprice";
}
标记中:
<p>
就像我上面说过的那样,我尝试了各种方法将值传递给JSP页面但没有工作,页面也不会加载。
我试图使用 <p th:text="${session.houseprice}" th:unless="${session == null}">[...]</p>
<p th:text="${session.housepricelistsize}" th:unless="${session == null}">[...]</p>
来传递值,如下所示:
model
我使用简单的 model.addAttribute("houseprice", housePrice);
model.addAttribute("housepricelistsize", number_of_houses);
将模型打印为调试,并打印了正确的值和值名称。
我尝试创建System.out.println(model);
实体并在控制器中创建这些对象的列表。然后我将使用HousePriceObject
并在控制器类的方法末尾返回ModelAndView
的名称而不是JSP页面名称(ModelAndView
),如下所示:
houseprice
使用 ModelAndView map = new ModelAndView("houseprice");
map.addObject("housepricesList", housepricesList);
// Return the ModelAndView instead of "houseprice" JSP page
return map;
循环遍历列表并将其打印在JSP页面上,但它仍然无法正常工作。
<c:forEach>
我在这里错过了一些小事,或者我是以完全错误的方式解决这个问题。正如我所说,值似乎已准备好发送,我认为它们会在页面加载后立即打印,但页面根本不会加载。建议将不胜感激!
编辑在调用方法时,我从控制器加载了其他JSP页面,但是上面强调的特定方法具有更多功能(即从一个JSP页面获取数据) ,查询数据库,并将数据打印到新的JSP页面等,有些东西会阻止<table>
<c:forEach items="${housepricesList}" var="HousePrice">
<tr>
<td><c:out value="${HousePrice.housePrice}"/></td>
<td><c:out value="${HousePrice.number_of_houses}"/></td>
</tr>
</c:forEach>
</table>
JSP页面打开。
例如,当我在没有所有额外代码的情况下运行上述方法时,JSP页面将会打开。
houseprice