该工程最初版本来自于Mybatis所在的google code站点,属于轻量级J2EE技术框架应用范围,使用了Spring、Stripes和MyBatis开源框架。其中Stripes为MVC框架的一种,该框架的设计和使用理念是零配置。熟悉该工程后主要是做了两方面的改进,一个是开发调试方面,另一个是数据库连接方面。
本文主要介绍开发调试方面:采用Jetty Web Server进行开发调试,效率更高。
方式1:maven命令行方式
该方式比较简单,只需在工程的pom文件里添加一个插件,其具体内容如下:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>**/*.jsp</exclude>
</excludes>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
</plugin>
方式2:嵌入式方式
该方式略微复杂,首先在工程的pom文件里添加JSP support configured,其具体内容如下:
<!-- JSP support configured -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-jetty</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20091210</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.1-glassfish</artifactId>
<version>2.1.v20091210</version>
</dependency>
其次,在工程的src/main/java/org/mybatis/jpetstore/web目录下新建一个webwork目录,并在该目录下新增一个PetStoreJettyServer.java,其具体内容如下:
package org.mybatis.jpetstore.web.webwork;
//Jetty 6
import java.io.File;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
public class PetStoreJettyServer {
private Server server;
//private static final Log LOG = new Log();
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) {
new PetStoreJettyServer();
}
// Jetty 6
public PetStoreJettyServer() {
//LOG.info("Starting web...");
try {
server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
String[] tryPaths = new String[] {
"mybatis-jpetstore/src/main/webapp",
"../mybatis-jpetstore/src/main/webapp"
};
String webapp = System.getProperty("webapp");
if (webapp == null) {
for (String tryPath : tryPaths) {
if (new File(tryPath).exists()) {
webapp = tryPath;
}
}
}
if (webapp == null) {
//LOG.severe("Could not find suitable web app sources to deploy, failing");
return;
}
WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath("/");
webappcontext.setWar(webapp);
HandlerCollection handlers= new HandlerCollection();
handlers.setHandlers(new Handler[]{webappcontext, new DefaultHandler()});
server.setHandler(handlers);
server.start();
try {
server.join();
} catch (InterruptedException e) {
//LOG.severe(e);
}
} catch (Exception e) {
//LOG.severe(e);
}
}
}
















