【发布时间】:2016-05-19 08:35:37
【问题描述】:
我创建了一个 JSP 页面,其中包含一些文本字段。单击提交按钮时,它调用 Ajax 调用将数据发布到 Spring 控制器。现在在数据库中保存数据后,控制器创建一个 JSON 响应。我希望这个响应在 JSP 文件中可用。以下是我文件的代码...
JSP 文件:
$(document).ready(function(){
$("#submitButton").click(function(e){
var formData = getFormData();
$.ajax({
type: 'POST',
'url': 'http://localhost:8080/Test_ReportingUI/addDb.htm',
data: {jsonData: JSON.stringify(formData)},
dataType: 'json',
success: function(response){
try{
var strResponse=jQuery.parseJSON(response);
}catch(err){}
if(response.status=='ok')
{
alert("details added");
}
else
{
alert("error happened");
}
},
timeout: 10000,
error: function(xhr, status, err){
if(status=='timeout')
{
alert('Request time has been out','');
}
console.log(status,err);
}
});
});
});
弹簧控制器方法:
@Autowired RWCustomerService rwCustomerService;
@RequestMapping (value="addDb.htm", method=RequestMethod.POST)
String addEditCustomer(@RequestParam String jsonData)
{
System.out.println("hello world");
System.out.println("-----------------\n"+jsonData);
ModelAndView modelAndView=new ModelAndView("custDB_setup");
JSONObject jsonResponse = new JSONObject();
try{
JSONObject requestedJSONObject = new JSONObject(jsonData);
String dbName = requestedJSONObject.getString("dbName");
String dbUserName = requestedJSONObject.getString("dbUserName");
String dbPassword = requestedJSONObject.getString("dbPassword");
Iterator it=requestedJSONObject.keys();
while(it.hasNext()){
System.out.println("Oo..: "+it.next());
}
RWReportCustomerDB rwReportCustomerDB = new RWReportCustomerDB();
rwReportCustomerDB.setDbName(dbName);
rwReportCustomerDB.setDbLogin(dbUserName);
rwReportCustomerDB.setDbPassword(dbPassword);
rwCustomerService.saveDb(rwReportCustomerDB);
jsonResponse.put("status", "ok");
}
catch(Exception ex){
ex.printStackTrace();
}
return jsonResponse.toString();
}
数据被保存在数据库中,因此 json 数据到达控制器方法。但是,我在 jsp 页面上看不到警告框,即“添加了详细信息”。我该如何解决这个问题。
【问题讨论】:
标签: java json spring jsp spring-mvc