I
I
IceR0ck2017-09-14 09:45:29
Spring
IceR0ck, 2017-09-14 09:45:29

Spring Thymeleaf how to properly return post form collection object?

Good afternoon, when returning an element of the collection, an error occurs. What is the correct way to return an element inside a collection loop? Didn't find anything on google.
There is a table where the collection is added

<!--/*@thymesVar id="users" type="ru.test.database.entity.User"*/-->
  <!--/*@thymesVar id="user" type="ru.test.database.entity.User"*/-->
<tr th:each="user : ${users}">
                    <td th:text="${user.id}" ></td>
                    <td th:text="${user.email}" ></td>
                    <td th:text="${user.firstName}" ></td>
                    <td th:text="${user.lastName}" ></td>
                    <td th:text="${user.level}" ></td>
                    <td th:text="${user.exp}" ></td>
                    <td th:text="${user.coins}" ></td>
                    <td><img th:src="${user.avatarPath}" /></td>
                    <td><form method="POST" enctype="multipart/form-data" action="#" th:action="@{/web-admin/deleteUser}" th:object="${user}"><button class="btn btn-danger">DELETE</button></form></td>
                </tr>

Controller
@GetMapping("/users")
    public String userForm(Model model){
        model.addAttribute("userAttr", new UserAttr());
        model.addAttribute("users", accountService.getAllUsers());
        return "users";
    }

@PostMapping("/users")
    public String userList(@ModelAttribute("userAttr") UserAttr user, Model model){
        try {
            accountService.addUser(user.getEmail(), user.getFirstName(), user.getLastName(), user.getAvatar(), user.getLevel(), user.getExp(), user.getCoins());
        } catch (Exception ex) {
            model.addAttribute("error", ex);
        }
        model.addAttribute("users", accountService.getAllUsers());
        return "users";
    }

    @PostMapping("/deleteUser")
    public String deleteUser(@ModelAttribute("user") User user, Model model){
        try {
            accountService.deleteUser(user.getId());
        } catch (Exception ex) {
            model.addAttribute("error", ex);
        }
        model.addAttribute("users", accountService.getAllUsers());
        return "users";
    }

object
@Entity
@Table(name = "users")
public class User{
    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private long id;
    @Column(name = "email")
    private String email;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "level")
    private int level;
    @Column(name = "exp")
    private long exp;
    @Column(name = "coins")
    private long coins;
    @Column(name = "avatar_path")
    private String avatarPath;

    public User() {
    }

    public User(String email, String firstName, String lastName, int level, long exp, long coins, String avatarPath) {
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.level = level;
        this.exp = exp;
        this.coins = coins;
        this.avatarPath = avatarPath;
    }

    public long getId() {
        return id;
    }
    public String getEmail() {
        return email;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public int getLevel() {
        return level;
    }
    public long getExp() {
        return exp;
    }
    public long getCoins() {
        return coins;
    }
    public String getAvatarPath() {
        return avatarPath;
    }

    public void setId(long id) {
        this.id = id;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setLevel(int level) {
        this.level = level;
    }
    public void setExp(long exp) {
        this.exp = exp;
    }
    public void setCoins(long coins) {
        this.coins = coins;
    }
    public void setAvatarPath(String avatarPath) {
        this.avatarPath = avatarPath;
    }
}

Mistake
2017-09-14 09:31:18.660 ERROR 1172 --- [io-2308-exec-10] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-2308-exec-10] Exception processing template "achievements": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (achievements:41)
2017-09-14 09:31:18.671 ERROR 1172 --- [io-2308-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (achievements:41)] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'achievementAttr' available as request attribute
  at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
  at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceR0ck, 2017-09-14
@IceR0ck

Thanks everyone I found the answer https://stackoverflow.com/questions/22794057/thyme...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question