Search code examples
javaspring-mvcfreemarkercuba-platform

Using ftl display list from POST method


Sorry for my english! I am new to Spring and FTL. I want to display firstName and lastName using <#list template, but I could not recognize any sequences in my POST method, could some one please explain to me. Again I am a newbie and please don't judge me if I don't understand what I should. I am using CUBA STUDIO 6.8 and IDEA. Also I'm working on this task in portal module

This is how I add firstName and lastName to database using my ftl form and Portal Controller:

@GetMapping("/add")
public String add(Model model){
    PersonPojo personPojo = new PersonPojo();
    model.addAttribute("personPojo", personPojo);
    return "add";
}

@PostMapping("/add")
public String save(Model model, @ModelAttribute("personPojo") PersonPojo personPojo){

    String firstName = personPojo.getFirstName();
    String lastName = personPojo.getLastName();
    PersonPojo newPerson = new PersonPojo(firstName, lastName);

    Person standardEntity = metadata.create(Person.class);
    standardEntity.setFirtName(newPerson.getFirstName());
    standardEntity.setLastName(newPerson.getLastName());
    dataManager.commit(standardEntity);

    return "redirect:/allPersons";
}

My ftl form:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post" name="person">
    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="Create">
</form></body>
</html>

Thank you!


Solution

  • So, If someone is interested i will post my solution here:

     @RequestMapping(value = "/allPersons", method = RequestMethod.GET)
        public String getPersons(Model model) {
                LoadContext loadJohn = new LoadContext(John.class);
                loadJohn.setQueryString("select u from test6$John u");
                model.addAttribute("users", dataService.loadList(loadJohn));
                return "list";
        }
    

    And the ftl should look like this: The problem i faced next was I did not know I have to check list for null. !"" does that

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h3>Person List</h3>
    <a href="/app-portal/add">Add Person</a>
    <br><br>
    <div>
        <table border="1">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
            <#list users as show>
                <tr>
                    <td>${show.firstName!""}</td>
                    <td>${show.lastName!""}</td>
                </tr>
            </#list>
        </table>
    </div>
    </body>
    </html>
    

    I hope this will help to people like me. Also, If someone knows how to delete and update data please share. Thanks!