我在将 Vaadin 与 Spring 应用程序集成时遇到一些问题。我的所有 bean 都在“rootcontext.xml”文件中。我可以通过实例化“rootcontext.xml”来调用 bean,然后调用我的服务类之一的 bean。
我可以通过这种方式填充表,但这是否是调用服务类的正确方法?因为我有更多的表必须调用它。
public final class TestTable extends Table {
private ApplicationContext applicationContext = (ApplicationContext) VaadinServlet.getCurrent().getServletContext()
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
private Service service = this.applicationContext.getBean("service",
Service.class);
public TestTable() {
service.findAll()
}
这是我的 UI 类:
@SpringUI
@Theme("dashboard")
@Widgetset("vaadin.DashboardWidgetSet")
public class TestUI extends UI {
/**
*
*/
private static final long serialVersionUID = -620721219079395670L;
private final DashboardEventBus dashboardEventbus = new DashboardEventBus();
@Override
protected void init(VaadinRequest request) {
setLocale(Locale.US);
DashboardEventBus.register(this);
Responsive.makeResponsive(this);
addStyleName(ValoTheme.UI_WITH_MENU);
updateContent();
// Some views need to be aware of browser resize events so a
// BrowserResizeEvent gets fired to the event bus on every occasion.
Page.getCurrent().addBrowserWindowResizeListener(new BrowserWindowResizeListener() {
@Override
public void browserWindowResized(final BrowserWindowResizeEvent event) {
DashboardEventBus.post(new BrowserResizeEvent());
}
});
}
private void updateContent() {
setContent(new MainView());
}
@WebServlet(urlPatterns = { "/TestUI/*", "/VAADIN/*" }, name = "TestUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = TestUI.class, productionMode = false)
public static class TestUIServlet extends VaadinServlet {
}
}
我的 root-context.xml 文件位于目录/WEB-INF/spring/root-context.xml 中。
Vaadin servlet 的 applicationContext.xml 位于目录/WEB-INF/spring/vaadin/applicationContext.xml 中。
这是我的 web.xml。 Vaadin Spring 教程说使用上下文加载器来初始化“applicationContext.xml”。我可以将其路径添加到 contextConfigLocation 参数,但应该只有一个根上下文。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
</web-app>
Vaadin 配置类:
import org.springframework.context.annotation.Configuration;
import com.vaadin.spring.annotation.EnableVaadin;
@Configuration
@EnableVaadin
public class VaadinConfiguration {
@Autowired
private Service service;
@Bean
public UI ui() {
System.out.println(service.findAll().size());
TestUI testUI = new TestUI();
testUI.setService(service);
return testUI;
}
}
最佳答案
我意识到的问题之一是我使用了错误的 Vaadin Spring 版本。我使用的是 Vaadin Spring 2.0,但它不适用于 Vaadin 7。所以我将其切换到 1.2。
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>1.2.0</version>
</dependency>
我将 Spring Vaadin Servlet 配置移至我的 web.xml
<servlet>
<servlet-name>UIServlet</servlet-name>
<servlet-class>com.vaadin.spring.server.SpringVaadinServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UIServlet</servlet-name>
<url-pattern>/UI/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UIServlet</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
我还必须向配置类和 UI 类添加一些 Vaadin Spring 注释。
@Configuration
@EnableVaadin
@EnableVaadinNavigation
public class VaadinConfiguration {
}
需要 @EnableVaadinNavigation 来启用 Spring View 的导航。
@SpringUI
@Theme("dashboard")
public class UI extends UI {
@Autowired
private SpringViewProvider viewProvider;
private final HorizontalLayout root = new HorizontalLayout();
private ComponentContainer content;
private Navigator navigator;
@Override
protected void init(VaadinRequest request) {
setLocale(Locale.US);
Responsive.makeResponsive(this);
addStyleName(ValoTheme.UI_WITH_MENU);
root.setSizeFull();
root.addStyleName("mainview");
setContent(root);
root.addComponent(new DashboardMenu());
content = new CssLayout();
content.addStyleName("view-content");
content.setSizeFull();
root.addComponent(content);
root.setExpandRatio(content, 1.0f);
navigator = new Navigator(this, content);
navigator.addProvider(viewProvider);
navigator.navigateTo(DashboardView.NAME);
}
}
我在 UI 中 Autowiring 了 SpringViewProvider,这也是识别使用 Spring View 注释的类所需要的。
所有配置都可以在这里找到:
https://vaadin.com/wiki/-/wiki/Spring+Vaadin/I+-+Getting+Started+with+Vaadin+Spring+and+Spring+Boot
关于java - 如何正确地将 Vaadin 与 Spring 框架集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055795/