用Jetty快速开发J2EE应用
对于用Maven管理的项目,可以使用Maven的Jetty插件启动应用进行快速测试。项目的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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cdai</groupId> <artifactId>struts2-conf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 主要依赖库的版本定义 --> <properties> <spring.version>3.1.2.RELEASE</spring.version> <mybatis.version>3.1.1</mybatis.version> <commons-dbcp.version>1.4</commons-dbcp.version> <jetty.version>7.6.5.v20120716</jetty.version> </properties> <dependencies> <!-- STRUTS2 begin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.1.2</version> </dependency> <!-- <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.1.2</version> </dependency> --> <!-- STRUTS2 end --> <!-- SPRING begin --> <!-- spring core begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- spring core end --> <!-- spring data access begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring data access end --> <!-- SPRING end --> <!-- MYBATIS begin --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <!-- MYBATIS end --> <!-- MYSQL begin --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <!-- MYSQL end --> <!-- COMMON begin --> <!-- web begin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- jetty for functional test and executable war --> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-webapp</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> <!-- web end --> <!-- persistence begin --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${commons-dbcp.version}</version> </dependency> <!-- persistence end --> <!-- aop begin --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> <scope>runtime</scope> </dependency> <!-- aop end --> <!-- COMMON end --> </dependencies> <build> <plugins> <!-- jetty插件, 设定context path与spring profile --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>development</value> </systemProperty> </systemProperties> <webAppConfig> <contextPath>/cdai</contextPath> </webAppConfig> </configuration> </plugin> </plugins> </build> </project>
之后在命令行输入mvn jetty:run即可启动应用程序。Jetty插件自动将src/main/webapp作为Web应用的根目录。
此外,也可以按照下面的方法用main()方法来启动应用。
package com.cdai.web.ssh; import java.io.File; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class JettyRunner { public static void main(String[] args) throws Exception { String contextPath = "/cdai"; int port = Integer.getInteger("port", 8080); Server server = createServer(contextPath, port); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); System.exit(100); } } private static Server createServer(String contextPath, int port) { //use Eclipse JDT compiler System.setProperty("org.apache.jasper.compiler.disablejsr199", "true"); Server server = new Server(port); server.setStopAtShutdown(true); String warFile = "src/main/webapp"; WebAppContext context = new WebAppContext(warFile, contextPath); context.setServer(server); //设置work dir,war包将解压到该目录,jsp编译后的文件也将放入其中。 String currentDir = new File(warFile).getParent(); File workDir = new File(currentDir, "work"); context.setTempDirectory(workDir); server.setHandler(context); return server; } }
但是在使用Struts2时,Jetty找不到struts2标签报错,不知道有没有人碰到过。为什么main()方法启动的Jetty不会自动去Jar中
找struts2标签配置文件呢?应该怎样配置Jetty去解决?
以下是Jetty插件默认的配置。存放JSP编译结果的临时目录不再是main()方法启动时的src/main/work,而是target/tmp。