将值从服务器分配给html并保存

时间:2015-07-03 13:48:22

标签: javascript jquery html spring spring-restcontroller

我使用春季休息。

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

@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组件赋值和反向

得到答案:

&#34; {&#34; timestamp&#34;:1436292452811,&#34; status&#34;:400,&#34;错误&#34;:&#34;错误请求&#34;,& #34; exception&#34;:&#34; org.springframework.http.converter.HttpMessageNotReadableException&#34;,&#34; message&#34;:&#34;无法读取文档:无法识别的令牌&#39; firstName& #39;:期待&#39; null&#39;,&#39; true&#39;,&#39; false&#39;或NaN \ n at [来源:java.io.PushbackInputStream@3f7cf4ca; line:1,column:11];嵌套异常是com.fasterxml.jackson.core.JsonParseException:无法识别的标记&#39; firstName&#39;:期待&#39; null&#39;,&#39; true&#39;,&#39; false&# 39;或NaN \ n at [来源:java.io.PushbackInputStream@3f7cf4ca; line:1,column:11]&#34;,&#34; path&#34;:&#34; / lodgers&#34;}&#34;

2 个答案:

答案 0 :(得分: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
                            }
                     }] 
}

请告诉我这是否是您正在寻找的

答案 1 :(得分:1)

表单看起来像这样

 <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对象