gpt4 book ai didi

java - 如何从 Controller 发送列表并在jsp中接受json

转载 作者:行者123 更新时间:2023-11-30 06:58:31 25 4
gpt4 key购买 nike

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

Controller 类

@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>

最佳答案

您似乎请求了错误的网址。在 jsp 的脚本中,传递的 url 为 $.ajax()是网址/RoomController/ViewMember.html 。但是在您的 Controller 中,您已将 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 发送为 "application/json" 。但是您将无法在映射为 ".html" 的 Controller 中接收带有 json 响应的 HTTP 200/".htm"在带有 Spring 3.2 或更高版本的 Spring MVC Controller 中。来自 https://stackoverflow.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)后缀,其目的是返回html并且不会回来 json也没有任何其他格式,即使您发送其他格式为 Accept header 。 ...

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

关于java - 如何从 Controller 发送列表并在jsp中接受json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com