简体   繁体   中英

Assign value from server to html and save

I use spring rest.

I want to show an object and save it.

@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;
    ...
}

On the html side what is the structure i need to use for the name? Are there some library which facilitate the process to assign value to the html component and the reverse

get answer:

"{"timestamp":1436292452811,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\\n at [Source: java.io.PushbackInputStream@3f7cf4ca; line: 1, column: 11]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\\n at [Source: java.io.PushbackInputStream@3f7cf4ca; line: 1, column: 11]","path":"/lodgers"}"

The input that needs to be sent here should be of the following format

{
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
                            }
                     }] 
}

Kindly let me know if this is what you are looking for

the form would look something like this

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

The jquery calling code would look like this

$(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 Code

@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();
}

The Person object must be converted to JSON. Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually. Because Jackson 2 is on the classpath, Spring's MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to JSON. The Person object sent to the controller will mapped by the jquery creating the Person object with the help of @RequestBody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM