本文介紹了如何讓ZK WebFragment與Embedded Jetty9一起工作?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
此最小的嵌入式Jetty項(xiàng)目正確啟動(dòng),掃描批注并查找并映射帶批注的TestServlet。
項(xiàng)目結(jié)構(gòu):
|-src/main/java/test
| |-Test.java
|-webapp/
| |-test.zul
|-pom.xml
Test.java:
package test;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
public class Test {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/test");
webapp.setBaseResource(Resource.newResource(new File("webapp").getCanonicalFile()));
// https://www.eclipse.org/jetty/documentation/jetty-9/index.html#configuring-webapps
// the order is important
webapp.setConfigurations(new Configuration[] { //
new WebInfConfiguration(), //
new WebXmlConfiguration(), //
new MetaInfConfiguration(), //
new FragmentConfiguration(), //
// new EnvConfiguration(), // not needed
// new PlusConfiguration(), // not needed
new AnnotationConfiguration(), //
// new JettyWebXmlConfiguration(), // no jetty-web.xml
});
webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*");
server.setHandler(webapp);
server.setDumpAfterStart(true);
server.start();
java.awt.Desktop.getDesktop().browse(new URI("http://localhost:8080/test/TestServlet")) /* working */;
java.awt.Desktop.getDesktop().browse(new URI("http://localhost:8080/test/test.zul")) /* not working */;
}
@WebServlet(urlPatterns = {"/TestServlet"})
public static final class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Test 1");
}
}
}
pom.xml:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>9.4.30.v20200611</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkbind</artifactId>
<version>9.6.0.1</version>
</dependency>
</dependencies>
</project>
test.zul:
<zk><label value="hello"/></zk>
ZK網(wǎng)頁(yè)片段似乎以某種方式被”記錄”了:
| +@ org.eclipse.jetty.webFragments.cache = java.util.concurrent.ConcurrentHashMap@bb35baa5{size=30}
| | +@ file:///C:/Users/r.hoehener/.m2/repository/org/zkoss/common/zcommon/9.6.0.1/zcommon-9.6.0.1.jar = org.eclipse.jetty.util.resource.EmptyResource@3fc39309
| | +@ file:///C:/Users/r.hoehener/.m2/repository/org/eclipse/jetty/jetty-annotations/9.4.30.v20200611/jetty-annotations-9.4.30.v20200611.jar = org.eclipse.jetty.util.resource.EmptyResource@3fc39309
| | +@ file:///C:/Users/r.hoehener/.m2/repository/org/eclipse/jetty/jetty-io/9.4.30.v20200611/jetty-io-9.4.30.v20200611.jar = org.eclipse.jetty.util.resource.EmptyResource@3fc39309
| | +@ file:///C:/Users/r.hoehener/.m2/repository/org/apache-extras/beanshell/bsh/2.0b6/bsh-2.0b6.jar = org.eclipse.jetty.util.resource.EmptyResource@3fc39309
| | +@ file:///C:/Users/r.hoehener/.m2/repository/org/zkoss/zk/zkwebfragment/9.6.0.1/zkwebfragment-9.6.0.1.jar = jar:file:///C:/Users/r.hoehener/.m2/repository/org/zkoss/zk/zkwebfragment/9.6.0.1/zkwebfragment-9.6.0.1.jar!/META-INF/web-fragment.xml
...
但test.zul顯示為純文本。ZK引擎未初始化。
知道為什么嗎?
編輯:為維護(hù)我進(jìn)行配置的方式:這直接來(lái)自9.x文檔,文檔中寫(xiě)著”您有許多選項(xiàng)可以讓Jetty使用不同的配置列表。”,包括”直接在WebAppContext上設(shè)置列表”:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="war"><SystemProperty name="jetty.base" default="."/>/webapps/my-cool-webapp</Set>
<Set name="configurationClasses">
<Array type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
<Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
</Array>
</Set>
</Configure>
推薦答案
首先,不要使用Jetty 9.4.30,它現(xiàn)在需要一些安全建議。
參見(jiàn):https://www.eclipse.org/jetty/security_reports.php
請(qǐng)至少使用Jetty 9.4.44.v20210927。
接下來(lái),檢查ZK Servlet的Jetty服務(wù)器轉(zhuǎn)儲(chǔ).
org.zkoss.zk.au.http.DHtmlUpdateServlet
org.zkoss.zk.ui.http.DHtmlLayoutServlet
如果WebAppContext轉(zhuǎn)儲(chǔ)中存在這些內(nèi)容,則Jetty已正確發(fā)現(xiàn)并加載了您的zkwebfragment-<ver>.jar。此時(shí),您需要做的就是如何使用ZK技術(shù)為您的ZK庫(kù)正確配置(從這里開(kāi)始您可以忽略Jetty特定的細(xì)節(jié))。
如果它們不存在,則首先確保您自己的Web應(yīng)用程序使用的是Servlet 3.0(在WEB-INF/web.xml中聲明)或更新版本,以獲得正確的Web片段支持(較早的Servlet規(guī)范不支持Web片段)。
接下來(lái),請(qǐng)確保WebApp類(lèi)加載器上存在zkwebfragment-<ver>.jar,因?yàn)楦鶕?jù)規(guī)范,不會(huì)從任何其他類(lèi)加載器加載Web片段,即使是應(yīng)用程序/服務(wù)器/容器類(lèi)加載器也不會(huì)加載。
如果您仍然沒(méi)有看到它們,則返回到調(diào)整默認(rèn)的Configuration列表,而不是代碼片段中的硬編碼列表。
(您的列表缺少必需的配置,并且成功的順序錯(cuò)誤,請(qǐng)不要更改默認(rèn)列表,不要在Web應(yīng)用上設(shè)置列表,只需更改服務(wù)器級(jí)別的默認(rèn)值)。
問(wèn)問(wèn)自己,ZK需要什么?(例如:如果需要jndi,那么您也需要jndi的具體配置件)。
如果您沒(méi)有停留在Java 8上,請(qǐng)使用Jetty 10,因?yàn)檎麄€(gè)Configuration層都經(jīng)過(guò)了修改,不再允許錯(cuò)誤的配置(事實(shí)上,舊的setConfiguration()方法甚至不在那里,僅僅是支持JAR的存在就足以標(biāo)記出您想要該支持,并在正確的位置、使用正確的父依賴(lài)項(xiàng)啟用它)。
這篇關(guān)于如何讓ZK WebFragment與Embedded Jetty9一起工作?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,






