但是,有时正确设置所有内容会有些困难。 因此,在本文中,我将向您快速概述如何将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