【问题标题】:Assign value from server to html and save将值从服务器分配给 html 并保存
【发布时间】:2015-09-21 09:23:16
【问题描述】:

我使用弹簧架。

我想显示一个对象并保存它。

@RequestMapping(value = "/lodgers", method = RequestMethod.POST)
public LodgerInformation createLodger(@RequestBody @Valid final LodgerInformation lodgerDto) {
    return lodgerService.save(lodgerDto);
}

public class LodgerInformation {
    private long lodgerId;
    private String firstName;
    private String lastName;
    private List<IdentityCardDto> identityCardDtoList;
    ...
}



public class IdentityCardDto {
    private long identityCardId;
    private IdentityCardTypeDto identityCardTypeDto;
    private String value;
    ...
}

public class IdentityCardTypeDto {
    private long identityCardTypeId;
    private String identityCardType;
    private Date expiration;
    private boolean hasExpirationDate=false;
    ...
}

在 html 方面,我需要为名称使用什么结构? 是否有一些库可以促进将值分配给 html 组件和反向的过程

得到答案:

"{"timestamp":1436292452811,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"无法读取文档:无法识别的令牌“firstName”:在 [Source: java.io.PushbackInputStream@3f7cf4ca;行:1,列:11];嵌套异常是 com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\n at [Source: java.io.PushbackInputStream@3f7cf4ca;行:1,列:11]","path":"/lodgers"}"

【问题讨论】:

  • 您在询问这里发送的 xml / json 转换为 LodgerInformation 吗?
  • 序列化表单似乎无法正常工作。

标签: javascript jquery html spring spring-restcontroller


【解决方案1】:

这里需要发送的输入应该是如下格式

{
lodgerId : 1,
identityCardDtoList: [{
                        identityCardId: 11,
                        identityCardTypeDto:{
                            identityCardTypeId: 111,
                            identityCardType: "Node 1.1.1",
                            expiration: 12/12/2015,
                            hasExpirationDate: true
                            }
                       },{
                        identityCardId: 12,
                        identityCardTypeDto:{
                            identityCardTypeId: 112,
                            identityCardType: "Node 1.1.2",
                            expiration: 12/12/2015,
                            hasExpirationDate: true
                            }
                     }] 
}

如果这是您要找的,请告诉我

【讨论】:

  • 更多的是我需要如何命名我的 html 组件。当我收到来自 ajax 调用的响应时,是否有库能够对组件进行分配......或者我需要类似 $("#lodgerFirstName").val(data.firstName);
  • 那么您是在询问数据发送到视图后如何在视图页面上赋值
  • 就像我说的,我的询问有两点。 1.我需要使用的组件名称才能为服务器分配值。还有一种方法可以将我从服务器获得的值分配给组件,或者我需要为每个字段手动分配。 2.如何获取表单值,构建我的json查询并将其传递给url。也许有一些库可以获取表单值并构建 json?
  • 想真正帮助你的伙伴,但很难理解你到底想要什么。
  • 如果我添加一个包含 50 个不同输入的表单,请选择...并需要将表单发送到休息服务。我需要手动构建 json 还是有一些库可以做到?
【解决方案2】:

表单看起来像这样

 <form id="newPersonForm">
      <label for="nameInput">Name: </label>
      <input type="text" name="name" id="nameInput" />
      <br/>
       
      <label for="ageInput">Age: </label>
      <input type="text" name="age" id="ageInput" />
      <br/>
      <input type="submit" value="Save Person" /><br/><br/>
      <div id="personFormResponse" class="green"> </div>
    </form>

jquery 调用代码如下所示

$(document).ready(function() {
// Save Person AJAX Form Submit
      $('#newPersonForm').submit(function(e) {
        // will pass the form data using the jQuery serialize function
        $.post('${pageContext.request.contextPath}/api/person', $(this).serialize(), function(response) {
          $('#personFormResponse').text(response);
        });
      });

    });

控制器代码

@Controller
@RequestMapping("api")
public class PersonController {

    PersonService personService;
// handles person form submit
@RequestMapping(value="person", method=RequestMethod.POST)
@ResponseBody
public String savePerson(@RequestBody Person person) {
    personService.save(person);
    return "Saved person: " + person.toString();
}

必须将 Person 对象转换为 JSON。感谢 Spring 的 HTTP 消息转换器支持,您无需手动进行此转换。因为 Jackson 2 在类路径中,所以会自动选择 Spring 的 MappingJackson2HttpMessageConverter 来将 Greeting 实例转换为 JSON。 发送到控制器的 Person 对象将由 jquery 在 @RequestBody 的帮助下创建 Person 对象进行映射

【讨论】:

  • 不让spring做转换,js中有没有办法做转换?
  • 当您有许多嵌套对象时,如果其中一些具有相同的名称字段会发生什么?结构是如何自动创建的?
猜你喜欢
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2023-03-27
相关资源
最近更新 更多