[英]send java array from jsp to controller in spring using ajax
我有一个jsp页面,在Java中有一个double []数组和一个按钮,这是JSP的代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
double[] beta = {1.2,4.3,1.0};
%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<button id="validate">Validate</button>
</body>
</html>
在验证按钮上单击,我正在进行ajax调用,该请求正在向控制器发送请求:
$('#validate').click(function() {
$.ajax({
url : 'controllerMethod'
type : 'POST'
data : ''
success:function(data){
}
});
});
我如何发送带有ajax请求的beta数组,以便可以在控制器方法中访问它。
@RequestMapping(value = "/controllerMethod", method = RequestMethod.POST)
@ResponseBody
public String validateSymbol(WebRequest webRequest,Model model,HttpServletRequest request, HttpServletResponse response) {
//beta;
}
只需在您的ajax数据中传递数组即可。 在这里我做data array
$('#validate').click(function() {
var data = [ "hello", "world" ];
$.ajax({
url : 'controllerMethod',
type: "POST",
dataType: 'json',
data: "data="+data,
success:function(data){
}
});
});
在控制器中使用@Requestparam
获取String数组,即String[] data
@RequestMapping(value = "/controllerMethod", method = RequestMethod.POST)
@ResponseBody
public String validateSymbol(WebRequest webRequest,Model model,HttpServletRequest request, HttpServletResponse response,@RequestParam String[] data) {
//beta;
//Here you will get your array in `data String array` which is passed from ajax call
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.