Hello sorry for my english, could you help me please. I have two forms (list and edit list) How can I display edit form based on 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:
import com.esotericsoftware.kryo.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import java.io.Serializable;
import java.util.UUID;
public class JohnPojo implements Serializable {
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;
}
}
Ftl form for editting:
<input type="hidden" name="id"> First Name: <input type="text" name="firstName"> <br> Last Name: <input type="text" name="lastName"> <br> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"> <input type="submit" value="Update">
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 edit form based on Id with data and save it.
Could you please tell me which POST and GET method should i write to achive my result. I am very strugling, because there is lack of documentaion for this specific task. Thanks!