ModelAttribute in form not able to bind with object (Spring MVC)

kw88 :

Currently, I am facing the problem of data binding using model attribute. (I am following the tutorial from https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation)

I have defined the modelattribute in form as well as the controller, but the error still appear (shown in following)

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'systemAccount' available as request attribute
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
    at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:201)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:748)

I have search for solution from the internet, but still haven't find the right solution for me.

The following is my code:

1.form code snippet

<form:form modelAttribute="systemAccount" action="/loginVali" method="post">
  <form:input path="username" />
  <form:input path="password" />
  <input type="submit" type="submit" value="submit"/>
</form:form>

2.controller code snippet

@Controller
public class GenericController {
    @RequestMapping(value="loginVali", method = RequestMethod.POST)
    public ModelAndView loginAuthentication(@ModelAttribute("systemAccount") SystemAccount sys,
                                            BindingResult bindingResult){
        System.out.println("-----------------------");
        System.out.println(sys.getUsername() + "   "+ sys.getPassword());
        System.out.println("-----------------------");
        return null; //for testing purpose, so I didn't put any operation
    }
}

3.entity code snippet (The entity is using the Hibernate framrwork).

@Entity
@Table(name = "SYSTEM_ACCOUNT")
public class SystemAccount {

    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="SYSTEM_ACCOUNT_SEQ")
    @SequenceGenerator(allocationSize=1,name="SYSTEM_ACCOUNT_SEQ", sequenceName="SYSTEM_ACCOUNT_SEQ")
    @Column(name = "ACCOUNT_ID")
    private int accountID;

    @Column(name = "USERNAME")
    private String username;

    @Column(name = "PASSWORD")
    private String password;

    @OneToOne(mappedBy="systemAccount", cascade = CascadeType.ALL)
    private Manager manager;

    @OneToOne(mappedBy="systemAccount", cascade = CascadeType.ALL)
    private CounterStaff counterStaff;

    @OneToOne(mappedBy="systemAccount", cascade = CascadeType.ALL)
    private Customer customer;

    @OneToOne(mappedBy="systemAccount", cascade = CascadeType.ALL)
    private Doctor doctor;

    public SystemAccount() {
    }

    public int getAccountID() {
        return accountID;
    }

    public void setAccountID(int accountID) {
        this.accountID = accountID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

    public CounterStaff getCounterStaff() {
        return counterStaff;
    }

    public void setCounterStaff(CounterStaff counterStaff) {
        this.counterStaff = counterStaff;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    @Override
    public String toString() {
        return "SystemAccount{" +
                "accountID=" + accountID +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", manager=" + manager +
                ", counterStaff=" + counterStaff +
                ", customer=" + customer +
                ", doctor=" + doctor +
                '}';
    }
}
kw88 :

Thank you Mr. M. Deinum! I have solved my issue!

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String displayLogin(Model model) {
        model.addAttribute("systemAccount", new SystemAccount());
        return "index";
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16623&siteId=1