gpt4 book ai didi

java - 如何使用 Controller 返回 bean 到 Ajax,并将它们显示在表格上?

转载 作者:行者123 更新时间:2023-11-30 06:19:29 26 4
gpt4 key购买 nike

我正在使用 SpringMVC,但遇到了问题。

我想做一个搜索方法。按钮可以通过Ajax将数据推送到 Controller ,并且确实有效!但我不知道如何在 View 表上显示bean列表。

JSP

<body>
<script>
function queryForm() {

$.ajax({
type: 'post',
url: '<%=request.getContextPath()%>/Student/searchStudent',
data:{stuNo: $('#stuNoSingle').val()},
dataType: "json",
success: function (data) {
alert("data");
},
error: function () {
alert("error");
}
});

}
</script>

<br> <br> 單一條件:學號查詢:
<table width="100%" border=1>
<tr>
<td>學號: <input type="text" id="stuNoSingle">
</td>
<td>
<button type="button" onclick="queryForm();">查詢</button>
</td>
</tr>
</table>

<br>
<br>
</body>

Controller

@Controller
@RequestMapping("/Student")
public class StudentController {

@Autowired
private StudentService studentService;

@RequestMapping("/searchStudent")
public @ResponseBody List<StudentBean> searchStudent(StudentBean StudentBean) throws Exception {
List<StudentBean> sbList = studentService.search(StudentBean);
System.out.println("controller List="+sbList);
return sbList;
}
}

最佳答案

您可以使用 jqGrid 执行此操作...下面的用户列表示例代码

var options=null;
var counter=0;
var URL = $('#contextpath').val() + '/page/User';
var selectedUser=null;
var enable = null;

(function($) {
options = $("#usergrid").jqGrid({
url:URL,
editUrl:URL,
datatype: 'json',
mtype: 'GET',
colNames:['Id', 'User ID', 'Name', 'Email'],
colModel:[
{name:'id',index:'id', editable:false, editoptions:{readonly:true, size:5}, hidden:true},
{name:'userid',index:'userid', width:90, editable:false},
{name:'name',index:'name', width:90, editable:true, editrules:{required:true}, editoptions:{size:10}},
{name:'email',index:'email', width:100, sortable:false, editable:true, editrules:{required:true}, editoptions:{size:10},formatter:returnEmailUserLink},
],
postData: {},
rowNum:10,
rowList:[10,20,40,60],
height: 'auto',
autowidth: true,
pager: '#userpager',
sortname: 'userid',
viewrecords: true,
sortorder: "asc",
emptyrecords: "Empty records",
multiselect: false,
loadonce: false,
//altRows: true,
loadComplete: function() {
$("tr.jqgrow:odd").css("background", "#f0f9ff");
$("tr.jqgrow:even").css("background", "#fff");
counter=0;
count = $('#usergrid').getGridParam("records");

if(count==0) {
$("#userrec").show();
}else{
$("#userrec").hide();
}
},
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "max",
repeatitems: false,
cell: "cell",
id: "id"
}
});

$("#usergrid").jqGrid('navGrid','#userpager',
{edit:false, add:false, del:false, search:false},
{}, {}, {}
);


})(jQuery);

关于java - 如何使用 Controller 返回 bean <list> 到 Ajax,并将它们显示在表格上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493916/

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