【问题标题】:Can you return a view from spring mvc as a string for an ajax call?你能从spring mvc返回一个视图作为ajax调用的字符串吗?
【发布时间】:2015-07-22 18:38:23
【问题描述】:

我正在尝试执行以下操作....

使用 ajax,我调用一个服务(一个 get 方法),该服务获取信息和所有...

我想要做的是,使用我的 ajax(不是 json,只是 GET,非常简单),得到从我之前调用的方法生成的视图(我打赌是字符串或 html)...

方法如下

@RequestMapping(value = "/info", method = RequestMethod.GET)
public  ModelAndView getInfo(@RequestParam(value = "id") String id) {
    ModelAndView mav = new ModelAndView();
    //do some calls to my services and return it to fill out the view

    mav.setViewName("dialogContent");
    mav.addObject("myObject", object);
    return mav;     
}

我也尝试过使用 @ResponseBody 注释...

到目前为止,它有效,只有在视图模板中只有 HTML 代码,但我需要显示通过“myObject”发送的信息,我收到一个错误,比如它是 null 或 undefined。这可能吗?或任何类似的方式来做到这一点?

谢谢。

【问题讨论】:

    标签: java ajax spring


    【解决方案1】:

    如果它是可序列化的,您可以在 SessionScope 中添加对象并在视图中检索它,否则我看不到这个工作,该对象不是同一请求的一部分,因此无法从您的调用页面访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2018-09-06
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多