如何从控制器发送列表并在 jsp 中接受 json

How to send list from controller and accept json in jsp

我想以 json 格式显示该列表。当我 运行 这段代码时,它不会以 json 格式显示任何数据,而是以其他格式显示。如何显示该列表?

控制器class

@Controller
public class RoomController {

@RequestMapping(method = RequestMethod.GET)
public ModelAndView saveEmployee(){ 
    System.out.println("welcome");
  return new ModelAndView("NewFile","message","hello");
 }
@RequestMapping(value="ViewMember",method=RequestMethod.GET)
public @ResponseBody List<RoomMembers> getRoomMembers() {
    System.out.println("second test");
    List<RoomMembers> roomMemberList= new ArrayList<RoomMembers>();
    roomMemberList=roomDao.listMember();
    return roomMemberList;
}

Jsp 文件是

<head>
<title>Spring MVC Ajax Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
function doAjax() {
  $.ajax({
    url: '/RoomController/ViewMember',
    type: 'GET',
    success: function(data) {
    var roommember=JSON.parse(data);;
      $('#time').html(roommember);
    }
  });
 }
</script>
</head>
<body>
<button id="demo" onclick="doAjax()" title="Button">Get the time!</button>
<div id="time">
</div>
</body>

看来您的请求有误url。在您的 jsp 中的脚本中,通过 do $.ajax() 的 url 是 url /RoomController/ViewMember.html。但是在您的控制器中,您已将 ir 映射为简单的 'ViewMember'

@RequestMapping(value="ViewMember",method=RequestMethod.GET,headers="Accept=application/json")

尝试只请求 /RoomController/ViewMember 让我们知道它是否有效。

编辑

你说 .html url 被调用并且你正在使用 Spring 4.0.6,并且你得到 HTTP 406 (NOTACCEPTABLE) 响应。

首先检查您是否发送 Accept header as "application/json"。但是您将无法在 Spring MVC 控制器中使用 ".html"/".htm" 映射的控制器中接收带有 json 响应的 HTTP 200 Spring 3.2 或以上。来自 https://whosebug.com/a/39479308/4190848:

As of Spring 3.2+, the content negotiation has other facts in account prior to eval Accept header:

From https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc:

Enabling Content Negotiation in Spring MVC

Spring supports a couple of conventions for selecting the format required: URL suffixes and/or a URL parameter. These work alongside the use of Accept headers. As a result, the content-type can be requested in any of three ways. By default they are checked in this order:

  • Add a path extension (suffix) in the URL. So, if the incoming URL is something like http://myserver/myapp/accounts/list.html then HTML is required. For a spreadsheet the URL should be http://myserver/myapp/accounts/list.xls. The suffix to media-type mapping is automatically defined via the JavaBeans Activation Framework or JAF (so activation.jar must be on the class path).
  • A URL parameter like this: http://myserver/myapp/accounts/list?format=xls. The name of the parameter is format by default, but this may be changed. Using a parameter is disabled by default, but when enabled, it is checked second.

  • Finally the Accept HTTP header property is checked. This is how HTTP is > actually defined to work, but, as previously mentioned, it can be problematic to use.

这实际上意味着如果您将 @Controller 方法映射到 .htm(l)后缀,本意是returnhtml不会return json 或任何其他格式,即使您将其他格式发送为 Accept header。 ...

因此,将映射更改为“.html”/“.htm”以外的其他后缀(或不使用后缀),您将解决错误

ajax中的url写错了,应该写成"ViewMember".