但是,有時正確設置所有內容會有些困難。 因此,在本文中,我將向您快速概述如何將Jetty與Weld for CDI和Vaadin一起設置為Web框架。
為了正確設置所有內容,我們需要執行以下步驟:
- 為所需的依賴項設置Maven Pom
- 創建一個基于Java的Jetty啟動器
- 設置web.xml
- 添加焊接占位符
為所需的依賴項設置Maven Pom
我使用以下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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>group.id</groupId><artifactId>artifact.id</artifactId><packaging>war</packaging><version>1.0</version><name>Vaadin Web Application</name><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><vaadin.version>6.7.1</vaadin.version><gwt.version>2.3.0</gwt.version><gwt.plugin.version>2.2.0</gwt.plugin.version></properties><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>gwt-maven-plugin</artifactId><version>${gwt.plugin.version}</version><configuration><webappDirectory>${project.build.directory}/${project.build.finalName}/VAADIN/widgetsets</webappDirectory><extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs><runTarget>cvgenerator-web</runTarget><hostedWebapp>${project.build.directory}/${project.build.finalName}</hostedWebapp><noServer>true</noServer><port>8080</port><compileReport>false</compileReport></configuration><executions><execution><goals><goal>resources</goal><goal>compile</goal></goals></execution></executions><dependencies><dependency><groupId>com.google.gwt</groupId><artifactId>gwt-dev</artifactId><version>${gwt.version}</version></dependency><dependency><groupId>com.google.gwt</groupId><artifactId>gwt-user</artifactId><version>${gwt.version}</version></dependency></dependencies></plugin><plugin><groupId>com.vaadin</groupId><artifactId>vaadin-maven-plugin</artifactId><version>1.0.2</version><executions><execution><configuration></configuration><goals><goal>update-widgetset</goal></goals></execution></executions></plugin></plugins></build><!-- extra repositories for Vaadin extensions --><repositories><repository><id>vaadin-snapshots</id><url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>vaadin-addons</id><url>http://maven.vaadin.com/vaadin-addons</url></repository></repositories><!-- repositories for the plugins --><pluginRepositories><pluginRepository><id>codehaus-snapshots</id><url>http://nexus.codehaus.org/snapshots</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></pluginRepository><pluginRepository><id>vaadin-snapshots</id><url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></pluginRepository></pluginRepositories><!-- minimal set of dependencies --><dependencies><dependency><groupId>com.vaadin</groupId><artifactId>vaadin</artifactId><version>${vaadin.version}</version></dependency><dependency><groupId>org.vaadin.addons</groupId><artifactId>stepper</artifactId><version>1.1.0</version></dependency><!-- the jetty version we'll use --><dependency><groupId>org.eclipse.jetty.aggregate</groupId><artifactId>jetty-all-server</artifactId><version>8.0.4.v20111024</version><type>jar</type><scope>compile</scope><exclusions><exclusion><artifactId>mail</artifactId><groupId>javax.mail</groupId></exclusion></exclusions></dependency><!-- vaadin custom field addon --><dependency><groupId>org.vaadin.addons</groupId><artifactId>customfield</artifactId><version>0.9.3</version></dependency><!-- with cdi utils plugin you can use Weld --><dependency><groupId>org.vaadin.addons</groupId><artifactId>cdi-utils</artifactId><version>0.8.6</version></dependency><!-- we'll use this version of Weld --><dependency><groupId>org.jboss.weld.servlet</groupId><artifactId>weld-servlet</artifactId><version>1.1.5.Final</version><type>jar</type><scope>compile</scope></dependency><!-- normally following are provided, but not if you run within jetty --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><type>jar</type><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><type>jar</type><scope>provided</scope></dependency><dependency><artifactId>el-api</artifactId><groupId>javax.el</groupId><version>2.2</version><scope>provided</scope></dependency></dependencies></project>
創建Java啟動器
有了這個pom,我們就有了一起運行Jetty,Vaadin和Weld所需的所有依賴項。 讓我們看一下Jetty Launcher。
import javax.naming.InitialContext;
import javax.naming.Reference;import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;/*** Simple jetty launcher, which launches the webapplication from the local* resources and reuses the projects classpath.* * @author jos*/
public class Launcher {/** run under root context */private static String contextPath = "/";/** location where resources should be provided from for VAADIN resources */private static String resourceBase = "src/main/webapp";/** port to listen on */private static int httpPort = 8081;private static String[] __dftConfigurationClasses ={"org.eclipse.jetty.webapp.WebInfConfiguration","org.eclipse.jetty.webapp.WebXmlConfiguration","org.eclipse.jetty.webapp.MetaInfConfiguration", "org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration","org.eclipse.jetty.webapp.JettyWebXmlConfiguration"} ;/*** Start the server, and keep waiting.*/public static void main(String[] args) throws Exception {System.setProperty("java.naming.factory.url","org.eclipse.jetty.jndi");System.setProperty("java.naming.factory.initial","org.eclipse.jetty.jndi.InitialContextFactory");InitialContext ctx = new InitialContext();ctx.createSubcontext("java:comp");Server server = new Server(httpPort);WebAppContext webapp = new WebAppContext();webapp.setConfigurationClasses(__dftConfigurationClasses);webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml");webapp.setContextPath(contextPath);webapp.setResourceBase(resourceBase);webapp.setClassLoader(Thread.currentThread().getContextClassLoader());server.setHandler(webapp);server.start();new Resource("BeanManager", new Reference("javax.enterprise.inject.spi.BeanMnanager","org.jboss.weld.resources.ManagerObjectFactory", null));server.join();}
}
此代碼將啟動一個Jetty服務器,該服務器使用項目中的web.xml來啟動Vaadin Web應用程序。 請注意,我們明確使用
setConfigurationClasses
操作。 這是確保我們具有可用于注冊Weld beanmanager的JNDI上下文所必需的。
設置web.xml
接下來,我們看一下web.xml。 接下來顯示我在此示例中使用的一個:
<?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>Vaadin Web Application</display-name><context-param><description>Vaadin production mode</description><param-name>productionMode</param-name><param-value>false</param-value></context-param><servlet><servlet-name>example</servlet-name><servlet-class>ServletSpecifiedByTheCDIVaadinPlugin</servlet-class><init-param><description>Vaadin application class to start</description><param-name>application</param-name><param-value>VaadinApplicationClassName</param-value></init-param><init-param><param-name>widgetset</param-name><param-value>customwidgetsetnameifyouuseit</param-value></init-param></servlet><servlet-mapping><servlet-name>example</servlet-name><url-pattern>/example/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list><listener><listener-class>org.jboss.weld.environment.servlet.Listener</listener-class></listener><resource-env-ref><description>Object factory for the CDI Bean Manager</description><resource-env-ref-name>BeanManager</resource-env-ref-name><resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type></resource-env-ref>
</web-app>
在web.xml的底部,您可以看到我們為Weld定義的resource-env和所需的偵聽器,以確保啟動Weld并注入了bean。 您還可以看到我們指定了一個不同的servlet名稱,而不是普通的Vaadin servlet。 有關此內容的詳細信息,請參見CDI插件頁面: https : //vaadin.com/directory#addon/cdi-utils
主要步驟是(從該頁面獲取):
- 在WEB-INF目錄下將空bean.xml -file(CDI標記文件)添加到您的項目中
- 將cdiutils * .jar添加到WEB-INF / lib下的項目中
- 通過擴展AbstractCdiApplication創建您的Application類
- 擴展AbstractCdiApplicationServlet并使用@WebServlet(urlPatterns =“ / *”)對其進行注釋
- 部署到與JavaEE / Web配置文件兼容的容器(CDI應用程序也可以在servlet容器等上運行,但需要進行一些進一步的配置)
添加焊接占位符
至此,我們已經擁有所有依賴項,我們創建了可直接從Eclipse使用的啟動器,并確保在啟動時加載了Weld。 我們還為Vaadin配置了CDI插件。 至此,我們差不多完成了。 我們只需要在我們要包含在Weld的bean發現中的位置添加空bean.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
我必須將這些添加到
src / main / java / META-INF
圖書館和 網絡信息 Weld的目錄以拾取所有帶注釋的bean。 就是這樣。 現在,您可以啟動啟動器,并且應該看到出現了所有的Weld和Vaadin日志記錄。
參考:來自JCG合作伙伴的 Embedded Jetty,Vaadin和Weld ? Smart Java博客中的Jos Dirksen。
翻譯自: https://www.javacodegeeks.com/2012/02/embedded-jetty-vaadin-and-weld.html