【问题标题】:Why ajax parsing will not allow Spring MVC controller to redirect jsp pages?为什么ajax解析不允许Spring MVC控制器重定向jsp页面?
【发布时间】:2016-01-06 20:19:08
【问题描述】:

我一直在控制器中使用ModelAndView作为对象类型来处理与前端html表单进行数据绑定的方法。例如:

@RequestMapping(value = "/postSth", method = RequestMethod.POST)
public ModelAndView postSomething(@RequestParam("name") String name){
       ModelAndView model = new ModelAndView("displayPage");
       model.addObject("name", name);
       return model;
}

此方法将允许我绑定name,并且我可以在POST 方法之后将name 显示到displayPage.jspdisplayPageJSP 页面名称,并使用 Spring 框架中的 InternalResourceViewResolver

最近我一直在使用 jQuery ajax 函数将数据传递给我的控制器,我正在研究的方法如下所示:

@RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public ModelAndView postSomething(@RequestBody String name){
       ModelAndView model = new ModelAndView("displayPage");
       model.addObject("name", name);
       return model;
}

我注意到我可以使用此控制器成功抓取JSON 字符串,但此方法不会将我的网页重定向到displayPage,并且与.addObject 的数据绑定不再起作用。

为什么它不起作用?如何将其更改为仍允许我定向到displayPage.jsp?我知道我可以在前端使用 javascript 进行重定向,但这不是我想要做的。

【问题讨论】:

  • 我不明白你的问题。如果您使用 ajax 发出请求,则该 ajax 请求的响应将包含由 displayPage 呈现的 html。
  • 这个方法行不通是什么意思?输出是不是displayPage 的内容?您是否收到错误响应?您是否收到了其他类型的回复?
  • @SotiriosDelimanolis displayPagejsp 页面,我试图在POST 之后重定向到,换句话说,当使用ajax 时,它会停止重定向到@987654342 @ 通常在没有 ajax 的情况下工作。
  • 您一直在使用术语redirect,但该术语的含义完全不同。您的意思是一个 Servlet RequestDispatcher forward,它只是呈现 JSP 的 HTML 并将其写入响应。如果您打印从您在 ajax 中发出的请求中收到的响应的内容,您会看到同样的内容。
  • @SotiriosDelimanolis 我认为重定向不是正确的词,你是正确的,我知道我收到了回复的内容。我想知道的是为什么 RequestDispatcher 在执行 ajax 时不再呈现 displayPage.jsp

标签: java json spring jsp spring-mvc


【解决方案1】:

我了解您在触发 ajax 调用时的要求,但您不需要将数据输出加载到当前 jsp 中,而是需要将其填充到新 jsp 中。

我有类似的需求,我使用 jquery load() 来实现它。在我的情况下它工作得很好。基本上它就像一个 3 步过程;

  1. 触发对控制器的 Ajax 调用,以获取在新 jsp 上加载所需的数据。
  2. 一旦需要的数据可用,就使用 load() 来加载当前页面中的新 jsp。(你可以使用一些 div 来加载它,如果你想完全替换当前页面,那么清空当前页面的内容并用 new 加载jsp)
  3. 编写 javascript/jquery 代码来操作我们在上一步渲染的新 JSP 中的 dom。

下面是如何完成的sn-p。

@RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public String postSomething(@RequestBody String name){

       return model;
}

<div id='currentContent'>
        <!--Existing content -->
</div>
var ajaxResult = '';
$.ajax({
    type : POST,
    url: "postSth",
    async:false}).then(function(data){
        //DIV ID here would be the ID which you need to have in your page to append the HTML content.
        //if you want to completely reload the page then use the id where your current contents are displayed
        //Inside your somenewjsp.jsp you can write your jquery codes to access the variable ajaxResult on document ready
        ajaxResult = data;
        $('#currentContent').load('/somenewjsp.jsp');
    });
});

请分享您对这种方法的结果。

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 2015-05-22
    • 2012-04-15
    • 2020-03-09
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    相关资源
    最近更新 更多