JSF2.0+JPA2.0+eclipse 構築編:現時点のプロジェクト構成
前のエントリでBootクラスの作りを見直し、更にweb.xmlをなくすという暴挙(?)に出たので、ここでいったんプロジェクトの中身を確認しておきます。
ファイルの内容
sandbox.server.ServletRegisterer
/** * */ package sandbo.server; import javax.faces.webapp.FacesServlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * @author jabaraster */ @WebListener public class ServletRegisterer implements ServletContextListener { /** * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) */ @Override public void contextDestroyed(final ServletContextEvent pSce) { // 処理なし } /** * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) */ @Override public void contextInitialized(final ServletContextEvent pSce) { final ServletContext context = pSce.getServletContext(); context.addServlet("jsfServlet", FacesServlet.class).addMapping("*.xhtml"); } }
サーブレット意外のものを登録したくなったらこの名前はふさわしくなくなりますが、今はこれでいきます。
sandbox.server.Boot
package sandbox.server; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import org.glassfish.api.deployment.DeployCommandParameters; import org.glassfish.api.embedded.ContainerBuilder; import org.glassfish.api.embedded.EmbeddedContainer; import org.glassfish.api.embedded.EmbeddedDeployer; import org.glassfish.api.embedded.EmbeddedFileSystem; import org.glassfish.api.embedded.LifecycleException; import org.glassfish.api.embedded.Server; /** * */ public class Boot { private static final String APPLICATION_NAME = "sandbox-application"; private static final String CONTEXT = "/"; private static final int PORT = 18080; private final String fileName; private EmbeddedDeployer deployer; private Server server; /** * @param pFileName */ public Boot(final String pFileName) { this.fileName = pFileName; } /** * */ public void boot() { try { start(); deploy(); } catch (final IOException e) { throw new IllegalStateException(e); } catch (final LifecycleException e) { throw new IllegalStateException(e); } } /** * */ public void redeploy() { System.out.println("再デプロイ開始…"); this.deployer.undeployAll(); deploy(); System.out.println("再デプロイ完了"); } /** * @throws LifecycleException */ public void stop() throws LifecycleException { System.out.println("アンデプロイ開始…"); this.deployer.undeployAll(); System.out.println("サーバー停止…"); this.server.stop(); System.out.println("サーバー停止完了"); } private void deploy() { System.out.println("デプロイ開始…"); this.deployer = this.server.getDeployer(); final DeployCommandParameters deployParams = new DeployCommandParameters(); deployParams.name = APPLICATION_NAME; deployParams.contextroot = CONTEXT; final File archive = new File(this.fileName); this.deployer.deploy(archive, deployParams); System.out.println("デプロイ完了"); } private void start() throws IOException, LifecycleException { System.out.println("サーバー起動開始…"); final Server.Builder serverBuilder = new Server.Builder("testBuilder"); final EmbeddedFileSystem.Builder fsBuilder = new EmbeddedFileSystem.Builder(); final EmbeddedFileSystem fs = fsBuilder.build(); serverBuilder.embeddedFileSystem(fs); this.server = serverBuilder.build(); final ContainerBuilder<EmbeddedContainer> containerBuilder = this.server .createConfig(ContainerBuilder.Type.web); this.server.addContainer(containerBuilder); containerBuilder.create(this.server); this.server.createPort(PORT); this.server.start(); System.out.println("サーバー起動完了"); } /** * @param args * @throws Exception */ public static void main(final String[] args) throws Exception { final Boot server = new Boot("WebContent"); server.boot(); final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for (String line = reader.readLine(); !"end".equals(line); line = reader.readLine()) { server.redeploy(); } server.stop(); } }
index.xhtml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" > <head> <meta charset="UTF-8" /> <title>JSF 2.0</title> </head> <body> <span jsfc="h:outputText">Hello,JSF!</span> </body> </html>
これで全部。かんたん。