我在调用成功执行后使用ajax调用来获取一些位置我希望将该数据传递给另一个jsp并在那里显示它。我的代码是
Ajax Call
$(".savebutton").click(function(){
$.ajax({
url: "<c:url value='/findPlaces' />",
type: 'POST',
data:{
preferences : preferences
},
success: function(response)
{
console.log(response);
window.location.href = "places";;
},
error: function(xhr, ajaxOptions, thrownError) {
alert('Error');
}
});
});
控制器操作
@RequestMapping(method = RequestMethod.POST, value = "/findPlaces")
public ModelAndView findRecommnededPlaces(HttpServletRequest request) {
Map< String, Integer> userPreferences = new HashMap< String, Integer>();
Map< String, Integer> sortedUserPreferences = new HashMap< String, Integer>();
userPreferences.put("Museum", Integer.parseInt(request.getParameter("preferences[Museum]")));
userPreferences.put("Night Life", Integer.parseInt(request.getParameter("preferences[Night Life]")));
userPreferences.put("Food", Integer.parseInt(request.getParameter("preferences[Food]")));
userService.scalePreferences(userPreferences);
sortedUserPreferences = userService.sortPreferences(userPreferences);
Map< String, Map< String, Integer >> categorisedPlaces = new HashMap< String, Map< String, Integer >>();
Map<String , List<String>> recommendedPlaces = new LinkedHashMap<String, List<String>>();
List<Place> places = searchService.search(new Location(SOURCE_LATITUDE,SOURCE_LONGITUDE), new Location(DEST_LATITUDE,DEST_LONGITUDE));
categorisedPlaces = placesService.categorize(places);
recommendedPlaces = placesService.getRecommendedPlaces(sortedUserPreferences, categorisedPlaces);
ModelAndView model = new ModelAndView("places");
model.addObject("recommendedPlaces", recommendedPlaces);
return model;
}
places.jsp
<div id="content">
<c:forEach var="entry" items="${recommendedPlaces}">
Cat: ${entry.key} <br/>
Places: ${entry.value}<br>
</c:forEach>
</div>
如何重定向到places.jsp并在那里传递recommndedPlaces,目前在ajax响应中我得到整个html页面
答案 0 :(得分:0)
我不明白为什么你要在AJAX
中进行调用,因为你想要将用户重定向到另一个页面...无论如何要将recommendedPlaces
作为{{1} }而不是JSON
页面,使用HTML
注释并返回@ResponseBody
而不是Map
对象:
ModelAndView
使用@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/findPlaces")
public Map<String, List<String>> findRecommnededPlaces(HttpServletRequest request) {
Map< String, Integer> userPreferences = new HashMap< String, Integer>();
Map< String, Integer> sortedUserPreferences = new HashMap< String, Integer>();
userPreferences.put("Museum", Integer.parseInt(request.getParameter("preferences[Museum]")));
userPreferences.put("Night Life", Integer.parseInt(request.getParameter("preferences[Night Life]")));
userPreferences.put("Food", Integer.parseInt(request.getParameter("preferences[Food]")));
userService.scalePreferences(userPreferences);
sortedUserPreferences = userService.sortPreferences(userPreferences);
Map< String, Map< String, Integer >> categorisedPlaces = new HashMap< String, Map< String, Integer >>();
Map<String , List<String>> recommendedPlaces = new LinkedHashMap<String, List<String>>();
List<Place> places = searchService.search(new Location(SOURCE_LATITUDE,SOURCE_LONGITUDE), new Location(DEST_LATITUDE,DEST_LONGITUDE));
categorisedPlaces = placesService.categorize(places);
recommendedPlaces = placesService.getRecommendedPlaces(sortedUserPreferences, categorisedPlaces);
return recommendedPlaces;
}
函数中的当前代码,响应对象中返回的success
将不会发送到recommendedPlaces
页面。您需要使用表单或将其添加到places.jsp
将其添加到请求中。这就是为什么我真的不明白你为什么要使用URL
来处理这个请求。