Java 在spring框架中,如何通过ajax调用将id值传递给控制器,从控制器获取对jsp页面的响应
在我的程序中,我需要通过传递id来执行一个操作。id是从下拉框中选择的(即动态更改)。因此,为了传递id,我正在使用ajax。但我不知道在spring中从controller接收响应的代码Java 在spring框架中,如何通过ajax调用将id值传递给控制器,从控制器获取对jsp页面的响应,java,ajax,spring-mvc,Java,Ajax,Spring Mvc,在我的程序中,我需要通过传递id来执行一个操作。id是从下拉框中选择的(即动态更改)。因此,为了传递id,我正在使用ajax。但我不知道在spring中从controller接收响应的代码 <div class="form-group"> <label class="col-sm-4 control-label">Employee</label> <div class="col-sm-6"> <select id="e
<div class="form-group">
<label class="col-sm-4 control-label">Employee</label>
<div class="col-sm-6">
<select id="empId" name="empId" class="form-control" >
<option value="" >--Select--</option>
<c:forEach var="row" items="${employeeList}" >
<option value="${employeeList.Id}">${employeeList.name</option>
</c:forEach>
</select>
</div>
<div class="col-sm-6">
<input type="text" name="empname" id="empname" class="form-control"/>
</div>
</div>
//i need to pass the employee id from the dropdown box to controller and get //the name of the employee as response .and set the response value in the text //box.how can be it done in spring using ajax.
这里是ajax端代码:
$('#empId').change(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'your_url', // in your controller side (RequestMapping value)
data: 'empId='+$('#empId').val(),
success: function(responseData, textStatus) {
// your stuff here. you found the response in responseData var.
},
complete: function(textStatus) {
},
error: function(responseData)
{
}
});
});
控制器端代码如下所示
@RequestMapping(value = "your_url", method = RequestMethod.POST)
public ResponseEntity<String> postMethod(HttpServletRequest req) {
String jsonString = "";
String empId = req.getParameter("empId");
// your operation done here and
//convert it to json format before sending response.
jsonString = gson.toJson("your response convert here to json format"); // here i used google Gson library to convert your response in json format.
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "text/html; charset=utf-8");
return new ResponseEntity<String>(jsonString, responseHeaders, HttpStatus.CREATED);
}
@RequestMapping(value=“your_url”,method=RequestMethod.POST)
公共响应postMethod(HttpServletRequest请求){
字符串jsonString=“”;
字符串empId=req.getParameter(“empId”);
//你的手术在这里完成了
//在发送响应之前将其转换为json格式。
jsonString=gson.toJson(“您的响应在此处转换为json格式”);//这里我使用google gson库将您的响应转换为json格式。
HttpHeaders responseHeaders=新的HttpHeaders();
add(“内容类型”,“text/html;charset=utf-8”);
返回新的ResponseEntity(jsonString、responseHeaders、HttpStatus.CREATED);
}
您想知道如何通过ajax调用控制器,以及控制器的方法是什么吗?我的意思是ajax调用和控制器端代码?@Ataur Rahman Munna:yesI粘贴一些示例代码。如果有帮助,请接受答案。谢谢。@Ataur Rahman Munna粘贴的代码是有用的。谢谢,然后您可以单击“接受”按钮接受我的答案。:)
@RequestMapping(value = "your_url", method = RequestMethod.POST)
public ResponseEntity<String> postMethod(HttpServletRequest req) {
String jsonString = "";
String empId = req.getParameter("empId");
// your operation done here and
//convert it to json format before sending response.
jsonString = gson.toJson("your response convert here to json format"); // here i used google Gson library to convert your response in json format.
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "text/html; charset=utf-8");
return new ResponseEntity<String>(jsonString, responseHeaders, HttpStatus.CREATED);
}