【问题标题】:How to properly integrate Vaadin with Spring Framework?如何正确地将 Vaadin 与 Spring Framework 集成?
【发布时间】:2017-10-18 17:49:22
【问题描述】:

我在将 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 tutorial 或较旧的wiki section?为了保持干净,表格应该只显示数据,其他人应该调用服务并向表格提供(填充)信息
  • applicationContext.xml 是用于 XML 定义的 spring 上下文的通用名称,但您可以随意命名,所以不,如果您已经拥有 rootcontext.xml,则无需创建另一个。您可以尝试的一件事是迁移到基于注释的上下文。这绝不是必要的,这是个人喜好,但我发现在编写代码时查看信息比必须转到 XML 定义更有帮助,即使现在 IDE 对这些东西有很好的 Spring 支持。
  • 根据您的评论,我假设您正在实例化 2 个不同的弹簧上下文。通常有一个这样的上下文包含所有 bean 定义,因此它可以在需要时正确地注入它们(除非您需要 hierarchy)。在 2 个不同的上下文中,一个人不知道另一个上下文中的内容。你可以做的是split it into smaller parts and import them in your root context
  • 保持原样,在web.xml 中引用root-context,并在root-context 中导入您的applicationContext,如previous link about splitting a context 中所述。如果我是你,我会完全迁移到基于注释的上下文(你已经有一些这样的 bean 和配置),而不再担心 XML。春季启动与否取决于您,vaadin tutorial 应提供有关如何启动和使用它的足够信息。
  • 你也可以从他们简单的vaadin-spring integration demo app中得到一些灵感,基于spring boot。

标签: java spring vaadin


【解决方案1】:

我意识到的一个问题是我为 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 视图启用导航。

@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 中自动装配了 SpringViewProvider,这也是识别使用 Spring View 注释的类所必需的。

所有配置都可以在这里找到:

https://vaadin.com/wiki/-/wiki/Spring+Vaadin/I+-+Getting+Started+with+Vaadin+Spring+and+Spring+Boot

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2015-05-27
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多