如何从spring控制器接收json响应到jsp页面

时间:2016-02-09 11:19:24

标签: java json spring jsp spring-mvc

我创建了一个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页面上看到警告框,即"详细信息已添加"。我该如何解决这个问题。

1 个答案:

答案 0 :(得分:5)

您需要添加@ResponseBody注释

@RequestMapping (value="addDb.htm", method=RequestMethod.POST)
@ResponseBody String  addEditCustomer(@RequestParam String jsonData)
{...}

您不需要手动编写JSON,Spring在场景后面使用Jackson库将对象序列化为JSON。见Spring 3 MVC and JSON example

  

添加@ResponseBody作为返回值。温泉看到了

     
      
  • 杰克逊图书馆已存在于项目类路径
  • 中   
  • mvc:annotation-driven已启用
  •   
  • 使用@ResponseBody
  • 注释的返回方法   
     

Spring将自动处理JSON转换。