1

我是 Spring MVC 的新手。挑战是我想将 JSON 或 XML 格式的所有表单数据发送到 Cotroller 方法。
Spring MVC 是否提供了开箱即用的功能?如果不是,那么我将如何接收 Controller 方法中的所有表单数据?在接收到它的所有表单数据后,我可以将其转换为 JSON 或 XML。
以我有限的经验,我可以在控制器方法的帮助下获得个人价值,@RequestPrameter但不确定如何接收完整的数据?

注意:我不想将表单值建模为任何模型对象。只想接收 JSON/XML 格式的值并将其作为 clob 保存到数据库中。

4

2 回答 2

0

在我看来,实现这一点的最佳方法是通过 http POST 请求以 json 格式发送数据,并在控制器方法中获取它:

    @RequestMapping(method = RequestMethod.POST, value = "/data", headers = "Accept=application/json") 
   @ResponseBody
    public List postData(@RequestBody String data) { {
        ...
        // here for example you can use GSON or Jackson to deserialize json to java instance
    }
于 2013-07-23T13:49:37.940 回答
0

There are two ways you can get complete form data to your Controller.

  1. You can get using request.getParameter("element_name");

Using this approach you are not using the Spring MVC's Form Element-Class Property Binding Feature.

  1. You can create POJO class of the element present inside your HTML Form.

In this approach you no need to get each value of form using request.getParameter("element_name"); instead you will get filled POJO object, which can be more handy to work on.

Also, you can handle data type binding errors more easily and in neat way.

For form element to class property binding refer, Best Help : http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/


For your requirement, you can't directly get in JSON/XML Format, Instead you need to call javascript function onSubmit and then create JSON/XML of form you needed and then you can submit the form.

于 2013-07-23T12:38:36.177 回答