嵌入式碼頭,Vaadin和焊接

當我開發Web應用程序時,我希望能夠從Eclipse快速啟動它們,而不必依賴各種重量級的tomcat或glassfish插件。 因此,我通常要做的只是創建一個可以直接從Eclipse運行的基于Java的簡單啟動器。 該啟動器會在幾秒鐘內啟動,因此使開發工作更加愉快。

但是,有時正確設置所有內容會有些困難。 因此,在本文中,我將向您快速概述如何將Jetty與Weld for CDI和Vaadin一起設置為Web框架。

為了正確設置所有內容,我們需要執行以下步驟:

  1. 為所需的依賴項設置Maven Pom
  2. 創建一個基于Java的Jetty啟動器
  3. 設置web.xml
  4. 添加焊接占位符

為所需的依賴項設置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

主要步驟是(從該頁面獲取):

  1. 在WEB-INF目錄下將空bean.xml -file(CDI標記文件)添加到您的項目中
  2. 將cdiutils * .jar添加到WEB-INF / lib下的項目中
  3. 通過擴展AbstractCdiApplication創建您的Application類
  4. 擴展AbstractCdiApplicationServlet并使用@WebServlet(urlPatterns =“ / *”)對其進行注釋
  5. 部署到與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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373328.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373328.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373328.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

創建真機調試證書(蘋果開發者平臺各個選項對應的含義)

創建真機調試證書&#xff08;蘋果開發者平臺各個選項對應的含義&#xff09; 原文地址&#xff1a;http://jingyan.baidu.com/article/ff411625b8141312e48237a7.html轉載于:https://www.cnblogs.com/siasyl/p/5340593.html

gl.vertexAtteib3f P42 講數據傳給location參數指定的attribute變量

參數  location  指定將要修改的attribute變量存儲位置 v0  指定填充attribute變量第一個分量的值 v1  指定填充attribute變量第二個分量的值 v2  指定填充attribute變量第三個分量的值 var VSHADER_SOURCE attribute vec4 a_Position;\n void main(){\n gl_Posit…

將Spring集成到舊版應用程序中

所有Spring開發人員喜歡做的事情之一就是將Spring塞入他們正在工作的任何應用程序中–這是我生活中的罪惡感之一&#xff1a;您看到一些代碼&#xff0c;認為它是垃圾&#xff0c;因為它包含幾個眾所周知的反模式&#xff0c;然后想想如果這個應用程序是Spring應用程序會多么酷…

java自己實現ioc_springioc原理、springmvc項目分析、自己實現IOC

從一個面試題開始&#xff1a;你自己實現IOC容器的話&#xff0c;保存bean你會使用什么數據結構來保存呢&#xff1f;現在的很多開發人員(甚至3年以上的)不一定能回答這問題&#xff0c;為什么會這樣呢&#xff1f;這個跟現在springboot現在已經高度成熟了&#xff0c;很多配置…

實現兩級下拉框的聯動

1.實現兩級下拉框的聯動。 功能&#xff1a;實現點擊年級下拉框&#xff0c;加載對應科目的下拉框。 第一步&#xff1a;首先要加載年級下拉框中的數據。 01.在GradeDAL層&#xff08;數據訪問層&#xff09;寫一個方法&#xff0c;查詢所有年級的信息。 /// <summary>//…

System.nanoTime()背后是什么?

在Java世界中&#xff0c;對System.nanoTime&#xff08;&#xff09;的理解非常好。 總有一些人說它是快速&#xff0c;可靠的&#xff0c;并且在可能的情況下&#xff0c;應該使用它代替System.currentTimemillis&#xff08;&#xff09;進行計時。 總的來說&#xff0c;他絕…

python連接SQL Server取多個結果集:Pymssql模塊

基本的用法可以參考&#xff1a;python連接SQL Server&#xff1a;Pymssql模塊 和上一篇文章中的代碼&#xff0c;只取一個結果集不同&#xff0c;這次會一次運行2個sql語句&#xff0c;然后分別取出2個結果集&#xff0c;打印輸出。 代碼中有詳細的注釋&#xff0c;一看就明白…

狀態不屬于代碼

Web應用程序中的“狀態”是什么&#xff1f; 它就是要存儲的數據&#xff08;無論目的地是什么—內存&#xff0c;數據庫&#xff0c;文件系統&#xff09;。 應用程序本身不得在代碼中存儲任何狀態。 這意味著您的類應僅包含帶有無狀態對象的字段。 換句話說&#xff0c;在程序…

Xen安全架構sHype/ACM策略配置圖文教程

實驗要求 1. 熟悉Xen虛擬化平臺部署&#xff1b; 2. Xen sHype/ACM安全架構中的Simple TE和Chinese Wall策略及事實上現機制的分析與驗證。 第1章 Xen環境部署 1.1 版本號選擇 因為Ubuntu使用廣泛。軟件包易于下載。我們選擇Ubuntu系統進行Xen部署…

Python 辨異 —— __init__ 與 __new__

__init__ 更多的作用是初始化屬性&#xff0c;__new__ 進行的是創建對象&#xff0c;顯然 __new__ 要早于 __init__ 發生。 考慮一個繼承自 tuple 的類&#xff0c;顯然在 __init__ 無法對其成員進行修改&#xff1b; class Edge(tuple):def __new__(cls, e1, e2):return tuple…

java彈出虛擬鍵盤_JS實現電腦虛擬鍵盤的操作

本文實例為大家分享了JS實現電腦虛擬鍵盤的具體代碼&#xff0c;供大家參考&#xff0c;具體內容如下需求&#xff1a;1.當輸入框光標聚焦時&#xff0c;電腦虛擬鍵盤彈出2.在輸入框輸入內容時&#xff0c;鍵盤跟著變化具體實現代碼如下&#xff1a;Html部分&#xff1a;電腦鍵…

Apache Mahout:入門

最近&#xff0c;我有一個有趣的問題要解決&#xff1a;如何使用自動化對不同來源的文本進行分類&#xff1f; 前一段時間&#xff0c;我讀到一個有關該項目以及許多其他文本分析工作的項目– Apache Mahout 。 盡管它不是一個非常成熟的版本&#xff08;當前版本為0.4 &#x…

Javascript中最常用的55個經典技巧(轉)

1. οncοntextmenu"window.event.returnValuefalse" 將徹底屏蔽鼠標右鍵 <table border οncοntextmenureturn(false)><td>no</table> 可用于Table 2. <body onselectstart"return false"> 取消選取、防止復制 3. οnpaste"…

向數組添加元素 java_java如何向數組里添加元素

向數組里添加一個元素怎么添加&#xff0c;這兒總結有三種方法&#xff1a;1、一般數組是不能添加元素的&#xff0c;因為他們在初始化時就已定好長度了&#xff0c;不能改變長度。但有個可以改變大小的數組為ArrayList&#xff0c;即可以定義一個ArrayList數組&#xff0c;然后…

JBoss Drools –入門

這篇文章是關于我如何掌握JBoss Drools的 。 其背后的原因是&#xff1a;SAP收購了我公司當前的規則引擎&#xff0c;而Drools是我們將尋找的另一種選擇&#xff0c;只要有人掌握了概念驗證的技能即可。 盡管似乎有大量的文檔&#xff0c;但是我總是會通過示例來發現它是有幫助…

android使用bintray發布aar到jcenter

前言 這兩天心血來潮突然想把自己的android library的aar放到jcenter里面&#xff0c;這樣一來自己便可以在任何時間任何地點通過internet得到自己的library的引用了&#xff0c;況且現在android studio已經默認使用jcenter的repositories作為依賴來源&#xff0c;以前的mavenc…

Java不是文明語言嗎?

幾周前&#xff0c;我有機會學習iOS編程。 我的老板認為我更像是“計算機科學家”&#xff0c;而不是開發人員&#xff0c;這意味著我可以將自己的知識應用于開發一兩個iPad應用程序–我要做的就是學習Objective-C&#xff0c; iOS SDK&#xff1a;到底有多難&#xff1f; 盡管…

PHP 進程詳解

PHP 進程詳解PHP 進程詳解 如下內容從《操作系統精髓與設計原理》中總結提煉得出&#xff0c;刪除了大部分對于理解進程有干擾的文字&#xff0c;對進程知識結構進行的梳理。幾乎所有內容為按照書本上摘抄下來的&#xff0c;我目前還總結提煉不出像作者這么深刻的見解。那么就先…

35. Search Insert Position

public class Solution {public int searchInsert(int[] nums, int target) {int lennums.length;int i0;for(;i<len;i){if(nums[i]>target)break;}return i;} } 轉載于:https://www.cnblogs.com/aguai1992/p/5351442.html

MySQL 后from多個表_MYSQL回顧(多表查詢相關)

前言簡單的數據我們可以直接從一個表中獲取&#xff0c;但在真實的項目中查詢符合條件的數據通常需要牽扯到多張表&#xff0c;這就不得不使用多表查詢。多表查詢分為多表連接查詢、符合條件鏈接查詢、子查詢。多表連接查詢包括內連接、外連接、全連接。符合條件連接查詢本質上…