【发布时间】:2014-12-25 19:59:33
【问题描述】:
我正在使用 undertow 开发一个 web 应用程序,并且对 java 还很陌生。运行 ServeletEngine.java 时出现错误。我检查了其他相关帖子,但他们没有解决我的问题。请帮我。
错误:[错误] 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project undertow-server: Compilation failure: Compilation failure:
我的目录结构是: ~/undertow-server/src/main/java/com/mastertheboss/undertow/ServeletEngine.java
我的 pom.xml 文件是: $ cat ~/undertow-server/pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mastertheboss.undertow</groupId>
<artifactId>undertow-server</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>undertow-server</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mastertheboss.undertow.ServeletEngine</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
ServeletEngine.java 位于: $ cat ~/undertow-server/src/main/java/com/mastertheboss/undertow/ServletEngine.java 包 com.mastertheboss.undertow; 导入 io.undertow.Undertow; 导入 io.undertow.server.*; 导入 io.undertow.util.Headers;
public class ServletEngine {
public static final String MYAPP = "/myapp";
public static void main(final String[] args) {
try {
DeploymentInfo servletBuilder = deployment()
.setClassLoader(ServletEngine.class.getClassLoader())
.setContextPath(MYAPP)
.setDeploymentName("test.war")
.addListener(new ListenerInfo(MySessionListener.class))
.addServlets(
servlet("App", App.class)
.addInitParam("message", "Hello World Implementing Servelets")
.addMapping("/myservlet"));
DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
manager.deploy();
HttpHandler servletHandler = manager.start();
PathHandler path = Handlers.path(Handlers.redirect(MYAPP))
.addPrefixPath(MYAPP, servletHandler);
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(path)
.build();
server.start();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果您需要更多信息,请告诉我。
【问题讨论】:
-
Java 的哪个版本?
-
java版本“1.8.0_25”
-
嗯...我不明白。我已经复制粘贴了您的两个文件,修复了 mainClass 名称并添加了一个简单的 MySessionListener 类和一个简单的 App servlet。你猜怎么了 ?当我启动
mvn compile exec:java时,它可以工作。 -
感谢 Alexis 试用。能帮我详细解答吗?您的 MySessionListener 类和 App serverlet 中有什么?他们的位置在哪里?
-
我已将这些类放在 com.mastertheboss.undertow 包中。 MySessionListener 实现 EventListener 并且为空。应用扩展 HttpServlet 并用 1 行覆盖 doGet:
resp.getWriter().write("OK");