【问题标题】:Vaadin application working from command line but not in IntelliJVaadin 应用程序从命令行工作,但不在 IntelliJ 中
【发布时间】:2015-07-06 13:22:55
【问题描述】:

实用工具:IntelliJ、Glassfish 服务器、Vaadin API

编辑:我应该提到这是我第一次使用 Vaadin

我有一个简单的 Vaadin 应用程序,我正在尝试启动它作为测试。如果我在命令行(使用 Maven)上运行它并从管理控制台的 Glassfish 应用程序面板启动它,它会完美运行。当我尝试在 IntelliJ 中使用相同的代码时,出现 404 错误。我想将 IntelliJ 用于调试目的,因此我也必须让它从那里工作。这是我正在使用的代码:

@Title("test")
@Theme("valo")
public class MyVaadinApplication extends UI {
    private String user;
    private String pwd;

    @WebServlet(value = "/*", asyncSupported = false)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinApplication.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {

        FormLayout form = new FormLayout();
        setContent(form);

        TextField nameField = new TextField("Name:");
        PasswordField passField = new PasswordField("Password:");
        Button button = new Button("Login");

        form.addComponent(nameField);
        form.addComponent(passField);
        form.addComponent(button);

        button.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                user = nameField.getValue();
                pwd = passField.getValue();
            }
        });

    }
}

这是 web.xml(它非常空且无关紧要)。据我了解,如果您使用@WebServlet 和@VaadinServletConfiguration 注释,则无需在web.xml 文件中指定任何映射/servlet。如果我错了,请有人纠正我。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>test</display-name>

<security-role>
    <role-name>admin</role-name>
</security-role>

<security-role>
    <role-name>user</role-name>
</security-role>

最后是我的 Maven pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>gov.bnl.cad</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
    <vaadin.version>7.4.2</vaadin.version>
    <vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
    <jetty.plugin.version>9.2.3.v20140905</jetty.plugin.version>
    <project.source.version>1.8</project.source.version>
    <project.target.version>1.8</project.target.version>
    <project.encoding>UTF-8</project.encoding>
</properties>
<distributionManagement>
    <repository>
        <id>CAD Repo</id>
        <url>file:///usr/common/jar/cad-repo</url>
    </repository>
    <site>
        <id>${project.artifactId}</id>
        <url>/usr/common/jar/cad-repo/${project.artifactId}</url>
    </site>
</distributionManagement>
<repositories>
    <repository>
        <id>CAD Repo</id>
        <url>file:///usr/common/jar/cad-repo/</url>
    </repository>
    <repository>
        <id>vaadin-addons</id>
        <url>http://maven.vaadin.com/vaadin-addons</url>
    </repository>
    <repository>
        <id>vaadin-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-server</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-themes</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-push</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client</artifactId>
        <version>${vaadin.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client-compiled</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client-compiler</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>gov.bnl.cad</groupId>
        <artifactId>pageParser</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>gov.bnl.cad</groupId>
        <artifactId>rcs-cad</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>


<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <classpathScope>compile</classpathScope>
                            <mainClass>com.vaadin.sass.SassCompiler</mainClass>
                            <arguments>
                                <argument>src/main/webapp/VAADIN/themes/dashbuilder/styles.scss</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <encoding>${project.encoding}</encoding>
                    <source>${project.source.version}</source>
                    <target>${project.target.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>${project.encoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <!-- Exclude some unnecessary files generated by the GWT compiler. -->
                    <packagingExcludes>WEB-INF/classes/VAADIN/gwt-unitCache/**,
                        WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.plugin.version}</version>
                <configuration>
                    <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
                    <webappDirectory>${basedir}/target/classes/VAADIN/widgetsets</webappDirectory>
                    <draftCompile>false</draftCompile>
                    <compileReport>false</compileReport>
                    <style>OBF</style>
                    <strict>true</strict>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>clean</goal>
                            <goal>resources</goal>
                            <goal>update-theme</goal>
                            <goal>update-widgetset</goal>
                            <goal>compile-theme</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
            </plugin>

            <!-- The Jetty plugin allows us to easily test the development build by
                running jetty:run on the command line. -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.plugin.version}</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 你是如何在 IntellIJ 的服务器上配置部署的?

标签: java maven intellij-idea glassfish vaadin


【解决方案1】:

使用 Servlet 3+,您根本不需要使用 web.xml,因为您的代码中提供了注释。因此,您可以根据需要删除该文件。如果您使用 web.xml,它将覆盖您的注释设置。

所以删除你的 web.xml 文件并在 @WebServlet 注释中将 value = "/*" 更改为 urlPatterns = "/*" 然后再试一次。

如果仍然无法正常工作,您需要明确检查您的 IntelliJ 运行配置。您的服务器可能未在 IDE 中正确配置。

【讨论】:

  • 感谢您的回复。不幸的是,我仍然收到 404 错误:/您在 pom 文件中看到任何错误或丢失的内容吗?我有一种感觉,我只是没有导入某些东西或没有使用正确的库。就我的服务器配置而言,一切都应该没问题。我在许多其他应用程序中使用过 Glassfish,它运行良好。我应该做/改变哪些 Vaadin 特定的事情?
  • 我在您的 Java 代码中看不到任何错误,并且您的应用程序可以从控制台正常运行,因此问题应该是特定于 IDE 的。您可能希望将您的 servlet 版本从 alpha 版本更新为更稳定的版本,但我认为这不会有太大变化。不过还是值得一试。除此之外,您的 pom.xml 似乎还不错。您可以在不同的 IDE(如 eclipse)或不同的服务器(如 Apache Tomcat)上对其进行测试,以缩小错误的可能性。如果 IntelliJ 没有给出任何编译时警告或错误,您的库导入应该没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2013-10-23
  • 1970-01-01
  • 2018-12-25
相关资源
最近更新 更多