Hello sorry for my english, but could you help me please. I have two forms (list and edit list) How can I display an edit form based on the id on the list? I use version 6.8 and IDEA, also working in Portal Module. For the form I use Freemarker(Ftl).
Here is my POJO class:
private String _csrf;
@NotBlank
private String id;
@NotBlank
private String firstName;
@NotBlank
private String lastName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String get_csrf() {
return _csrf;
}
public void set_csrf(String _csrf) {
this._csrf = _csrf;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
Controller for adding and edditing:
@GetMapping("/add")
public String add(Model model) {
John john = new John();
model.addAttribute("john", john);
return "add";
}
@PostMapping("/add")
public String save(@ModelAttribute("john") John john) {
String firstName = john.getFirstName();
String lastName = john.getLastName();
John newJohn = new John(firstName, lastName);
John standardEntity = metadata.create(John.class);
standardEntity.setFirstName(newJohn.getFirstName());
standardEntity.setLastName(newJohn.getLastName());
dataManager.commit(standardEntity);
return "redirect:/allPersons";
}
@RequestMapping(value = "/allPersons", method = RequestMethod.GET)
public String getPersons(Model model) {
List<John> johns = dataManager.loadList(LoadContext.create(John.class)
.setQuery(LoadContext.createQuery("select u from test6$John u")));
List<JohnPojo> johnPojos = johns.stream().map(e -> {
JohnPojo johnPojo = new JohnPojo();
johnPojo.setFirstName(e.getFirstName());
johnPojo.setLastName(e.getLastName());
johnPojo.setId(e.getId().toString());
return johnPojo;
}).collect(Collectors.toList());
model.addAttribute("users", johnPojos);
return "list";
}
When i go to Url: @GetMapping("/update/{id}"), I want to return the edit form based on id with data and save it. Could you please tell me which POST and GET method I should write to achieve the desired result. I am really struggling because there is lack of documentaion for this specific task. Thanks!
Not sure where exactly you're stuck, but it seems you'll need to use your dataManager to load the entity. Something like:
@GetMapping("/update/{id}")
public String getForUpdate(@PathVariable String id, Model model) {
John john = dataManager. load(id);
model.addAttribute("john", john);
return "update";
}