spring 如何将Vaadin添加到现有的Sping Boot 项目中,并使Vaadin实际启动/构建我的前端加路由?

vxqlmq5t  于 2023-05-21  发布在  Spring
关注(0)|答案(1)|浏览(135)

见鬼
我有一个Spring-boot项目,目前没有任何前端/UI。
Here是一个公共的代码库,你可以找到所有的代码。请注意,如果没有在www.example.com文件/环境变量中设置SQL服务器,它将无法运行application.properties。
Here是实际使用/启动Vaadin并构建前端的项目的另一个repo。
我尝试将Vaadin依赖项添加到我的POM.xml中。
以下是我添加的依赖项:

<properties>
        <java.version>11</java.version>
        <vaadin.version>24.0.5</vaadin.version>
    </properties>
    <!--adding vaadin!-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <!-- declare the latest Vaadin version
                     as a property or directly here -->
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!--adding vaadin!-->
    <dependencies>

        <!-- adding vaadin!-->
        <dependency>
            <groupId>com.vaadin</groupId>
            <!-- Replace artifactId with vaadin-core to use only free components -->
            <artifactId>vaadin</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <version>${vaadin.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.vaadin/vaadin-maven-plugin -->
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-maven-plugin</artifactId>
            <version>24.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring</artifactId>
            <version>3.2.1</version>
        </dependency>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-frontend</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>

我还添加了一个简单的类LoginView,以尝试路由和ui是否工作:

@Route(value = "login")
@PageTitle("Login | University Project")
@AnonymousAllowed
public class LoginView extends VerticalLayout implements BeforeEnterObserver {

    private final LoginForm login = new LoginForm();

    public LoginView() {

        addClassName("login-view");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);
        getStyle().set("background", "869fcb");
        Button login1 = new Button("Login");
        login.setAction("login");

        //////////////
        //HorizontalLayout back = new HorizontalLayout();
        //back.setSizeFull();
        //back.getStyle().set("background","red");
        //////////////
        add(toggleButtonTheme, new H1("University Project"), login, registrationButton);
    }

    Button toggleButtonTheme = new Button("Toggle dark theme", click -> {
        ThemeList themeList = UI.getCurrent().getElement().getThemeList(); // (1)

        if (themeList.contains(Lumo.DARK)) { // (2)
            themeList.remove(Lumo.DARK);
        } else {
            themeList.add(Lumo.DARK);
        }
    });

    Button registrationButton = new Button("Register", event -> UI.getCurrent().navigate("registration"));

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        // inform the user about an authentication error
        if (beforeEnterEvent.getLocation()
                .getQueryParameters()
                .getParameters()
                .containsKey("error")) {
            login.setError(true);
        }

    }
}

然后“localhost:8888/login”页面返回“Whitelabel Error Page”...
我尝试运行“mvn clean install”/“mvn package”/“mvn deploy”/等。- 所有的maven命令,然后从jar本身运行应用程序- Vaadin不像this tutorial那样启动。
我还尝试在运行配置之前添加“vaadin:build-frontend”和其他vaadin插件命令,但它不能启动Vaadin。
预期结果/运行日志:

##excluded some log events here##

2023-05-18 10:34:24.155  INFO 20288 --- [  restartedMain] c.v.f.s.DefaultDeploymentConfiguration   : 
Vaadin is running in DEVELOPMENT mode - do not use for production deployments.
2023-05-18 10:34:24.212  INFO 20288 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-18 10:34:24.225  INFO 20288 --- [  restartedMain] com.uniproject.application.Application   : Started Application in 8.524 seconds (JVM running for 9.355)
2023-05-18 10:34:24.230  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Starting Vite
2023-05-18 10:34:24.853  INFO 20288 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-18 10:34:24.853  INFO 20288 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-18 10:34:24.855  INFO 20288 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

------------------ Starting Frontend compilation. ------------------
2023-05-18 10:34:33.325  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Running Vite to compile frontend resources. This may take a moment, please stand by...
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   :   VITE v3.2.4  ready in 3377 ms

----------------- Frontend compiled successfully. -----------------

2023-05-18 10:34:36.774  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Started Vite. Time: 12544ms
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:36.775  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   :   ➜  Local:   http://127.0.0.1:61319/VAADIN/
2023-05-18 10:34:38.045  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:38.046  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : [TypeScript] Found 0 errors. Watching for file changes.

实际结局/运行日志:

##excluded some event logs here##

2023-05-18 10:35:50.078  INFO 18152 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-05-18 10:35:50.078  INFO 18152 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-05-18 10:35:51.601  INFO 18152 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-18 10:35:51.791  INFO 18152 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 174 ms. Found 8 JPA repository interfaces.
2023-05-18 10:35:52.973  INFO 18152 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2023-05-18 10:35:52.985  INFO 18152 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-18 10:35:52.985  INFO 18152 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2023-05-18 10:35:53.139  INFO 18152 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-18 10:35:53.139  INFO 18152 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3060 ms
2023-05-18 10:35:53.389  INFO 18152 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-18 10:35:53.459  INFO 18152 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.29.Final
2023-05-18 10:35:53.639  INFO 18152 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-05-18 10:35:53.823  INFO 18152 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-05-18 10:35:54.118  INFO 18152 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-05-18 10:35:54.158  INFO 18152 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
2023-05-18 10:35:55.117  INFO 18152 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-18 10:35:55.127  INFO 18152 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-18 10:35:56.214  WARN 18152 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-18 10:35:56.476  INFO 18152 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2023-05-18 10:35:56.495  WARN 18152 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : Unable to start LiveReload server
2023-05-18 10:35:56.795  INFO 18152 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
2023-05-18 10:35:57.445  INFO 18152 --- [  restartedMain] com.finals.cinema.CinemaApplication      : Started CinemaApplication in 8.049 seconds (JVM running for 9.291)

谢谢你的帮助提前。

aelbi1ox

aelbi1ox1#

Vaadin 24需要Sping Boot 3.x,但似乎您的项目使用的是2.4。您应该升级Sping Boot 或使用Vaadin 23.3

相关问题