ajax调用后重定向到另一个jsp(带响应数据)

时间:2014-04-12 15:34:37

标签: java ajax spring spring-mvc redirect

我在调用成功执行后使用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页面

1 个答案:

答案 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来处理这个请求。