我刚接触Vaadin和Spring Boot时,我正在尝试制作一个可以执行所有CRUD方法的小型应用程序。我正在制作此表单,该表单将接收字段中的所有数据,将其保存到模型对象中,然后将其保存到我的数据库中,但它不允许我这样做。
学生回购
package com.vinh.vaadin.allinone.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.vinh.vaadin.allinone.entity.Student;
@Repository
public interface StudentRepo extends JpaRepository<Student, Long>{
}
学生表格
package com.vinh.vaadin.allinone.vaadin;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.data.Binder;
import com.vaadin.data.ValidationException;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vinh.vaadin.allinone.entity.Student;
import com.vinh.vaadin.allinone.repo.StudentRepo;
@SpringComponent
public class StudentForm extends FormLayout{
@Autowired
StudentRepo repo;
Student student = new Student();
private TextField firstName = new TextField("First Name");
private TextField lastName = new TextField("Last Name");
private TextField email = new TextField("Email");
private Button save = new Button("Save");
private Button delete = new Button("Delete");
private Binder<Student> binder = new Binder<>(Student.class);
public StudentForm() {
HorizontalLayout buttons = new HorizontalLayout(save, delete);
addComponents(firstName, lastName, email, buttons);
binder.bindInstanceFields(this);
binder.setBean(student);
setSizeUndefined();
save.addClickListener(event -> {
try {
binder.writeBean(student);
} catch (ValidationException e) {
e.printStackTrace();
} finally {
saveStudent(student);
}
});
}
private void saveStudent(Student theStudent) {
repo.save(theStudent);
}
}
StudentForm接到电话的主要内容
package com.vinh.vaadin.allinone.vaadin;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import com.vinh.vaadin.allinone.entity.Student;
import com.vinh.vaadin.allinone.repo.StudentRepo;
import javassist.expr.NewArray;
@SpringComponent
public class MainContent extends VerticalLayout {
@Autowired
private Student student;
@Autowired
StudentRepo repo;
private StudentForm studentForm = new StudentForm();
private CssLayout filterLayout;
private TextField filterText;
private Button filterBtn;
private HomeUI homeUI;
private Grid<Student> studentGrid;
@PostConstruct
void init() {
setStudents(repo.findAll());
}
private void setStudents(List<Student> findAll) {
studentGrid.setItems(findAll);
}
public MainContent() {
addFilter();
addMainContent();
}
private void addMainContent() {
HorizontalLayout main = new HorizontalLayout();
studentGrid = new Grid<>(Student.class);
studentGrid.setColumns("firstName", "lastName", "email");
main.addComponentsAndExpand(studentGrid);
main.addComponents(studentForm);
studentGrid.setSizeFull();
addComponents(main);
}
private void addFilter() {
filterLayout = new CssLayout();
filterLayout.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
filterText = new TextField();
filterText.setPlaceholder("filter by name...");
filterBtn = new Button(VaadinIcons.CLOSE);
filterLayout.addComponents(filterText, filterBtn);
HorizontalLayout toolbar = new HorizontalLayout(filterLayout);
addComponent(toolbar);
}
}
总结中,我拥有MainContent组件,该组件在左侧显示一个网格,然后在右侧显示StudentForm。
一切正常,直到我尝试在StudentForm内repo.save(theStudent),并且出现此错误
java.lang.NullPointerException: null
at com.vinh.vaadin.allinone.vaadin.StudentForm.lambda$0(StudentForm.java:44) ~[classes/:na]
at sun.reflect.GeneratedMethodAccessor309.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_171]
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:499) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:273) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:237) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1014) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.ui.Button.fireClick(Button.java:384) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.ui.Button$1.click(Button.java:57) ~[vaadin-server-8.4.1.jar:8.4.1]
at sun.reflect.GeneratedMethodAccessor308.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_171]
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116) ~[vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:445) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:410) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1601) [vaadin-server-8.4.1.jar:8.4.1]
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:445) [vaadin-server-8.4.1.jar:8.4.1]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_171]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_171]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]
但是,如果将
repo.save()
放在saveStudent()
的MainContent
方法内,将该MainContent
传递给StudentForm
,然后从那里调用saveStudent()
方法,它将起作用。为什么?我在MainContent中遇到的另一个问题是,必须在
repo.findAll()
中添加@PostConstruct
才能使其正常工作,如果我将其放置在不是@PostConstruct的其他地方,那么它将无法正常工作,这是为什么? 最佳答案
您的StudentRepo类应使用@Component注释,因为您是在StudentForm类中创建其bean。
创建StudentForm的bean时,它需要未创建的StudentRepo类的bean,因为尚未将其标记为@Component。因此,当您尝试访问StudentRepo的方法时,将引发空指针异常
@Component
public interface StudentRepo extends JpaRepository<Student, Long> {
}
您还可以使用@Repository批注将其指定为dao图层类。
@Repository从@Component扩展而来,仅用于保持命名一致。
因此,两个注释都将起作用