В настоящее время я сталкиваюсь с проблемой привязки данных с использованием атрибута модели. (Я следую учебному пособию от https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation)
Я определил атрибут модели в форме, а также контроллер, но ошибка все еще появляется (показано ниже)
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)
У меня есть поиск решения по inte rnet, но я все еще не нашел подходящее решение для меня.
Вот мой код:
фрагмент кода 1.form
<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
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. код объекта фрагмент (сущность использует фреймворк Hibernate).
@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 +
'}';
}
}