Eclipse RCP插件+嵌入式Jetty+JSF

45

我制作了一个带有嵌入式Jetty的RCP插件,步骤如下:

1)在plugin.xml -> Dependencies中,我添加了以下内容:

org.eclipse.equinox.http.jetty
org.eclipse.equinox.http.registry
org.mortbay.jetty.server
javax.servlet

2)我在plugin.xml->扩展中添加了一个Servlet扩展点(org.eclipse.equinox.http.registry.servlet)。

class: TemperatureServlet
alias:/temperature

TemperatureServlet 的代码如下:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TemperatureServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        System.out.println("doGet Called");

        resp.sendRedirect("Convertor.jsp");
    }
}

这个文件名为Convertor.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h:form>
        <h:panelGrid columns="2">
            <h:outputLabel value="Celsius"></h:outputLabel>
            <h:inputText  value="#{temperatureConvertor.celsius}"></h:inputText>
        </h:panelGrid>
        <h:commandButton action="#{temperatureConvertor.celsiusToFahrenheit}" value="Calculate"></h:commandButton>
        <h:commandButton action="#{temperatureConvertor.reset}" value="Reset"></h:commandButton>
        <h:messages layout="table"></h:messages>
    </h:form>


    <h:panelGroup rendered="#{temperatureConvertor.initial!=true}">
    <h3> Result </h3>
    <h:outputLabel value="Fahrenheit "></h:outputLabel>
    <h:outputLabel value="#{temperatureConvertor.fahrenheit}"></h:outputLabel>
    </h:panelGroup>
</f:view>
</body>
</html>

文件 faces-config.xml 包含:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
 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/web-facesconfig_2_0.xsd"
    version="2.0">  
    <managed-bean>
        <managed-bean-name>temperatureConvertor</managed-bean-name>
        <managed-bean-class>hellojsf.TemperatureConvertor</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

</faces-config>

我的插件具有以下层次结构:

plugin-name
---src
------class package
---------Activator.java
---------Application.java
---------ApplicationActionBarAdvisor.java
---------ApplicationWorkbenchWindowAdvisor.java
---------Perspective.java
---------TemperatureConvertor.java
---------TemperatureServlet.java
---META-INF
------MANIFEST.MF
---resources
-------WebContent
----------WEB-INF
-------------faces-config.xml
-------------web.xml
----------Convertor.jsp
---plugin.xml

Activator 类中的 start() 方法中,我这样启动了 Web 服务器:

public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;

        Bundle bundle = Platform.getBundle("org.eclipse.equinox.http.registry");
        if (bundle.getState() == Bundle.RESOLVED) {
            bundle.start(Bundle.START_TRANSIENT);
        }

        Dictionary settings = new Hashtable();
        settings.put("http.enabled", Boolean.TRUE);
        settings.put("http.port", 8080);
        settings.put("http.host", "0.0.0.0");
        settings.put("https.enabled", Boolean.FALSE);
        settings.put("context.path", "/");
        settings.put("context.sessioninactiveinterval", 1800);

        try {
            JettyConfigurator.startServer(PLUGIN_ID + ".jetty", settings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
我已经在这个插件中添加了以下库:
  • JSTL: javax.servlet.jsp.jstl-1.2.1-javadoc.jar; javax.servlet.jsp.jstl-api-1.2.1-javadoc.jar
  • JSF 2.0 (Apache MyFaces JSF Core-2.0 API 2.0.2)
当我启动应用程序后,在浏览器中输入localhost:8080/temperature,它不知道Convertor.jsp在哪里。我的问题是:我如何配置此插件以了解WebContent资源位置,更重要的是,我如何配置插件以了解如何处理JSF并了解faces-config.xml和web.xml。
例如,当我定义扩展org.eclipse.equinox.http.registry.servlets时,我可以做什么? class:javax.faces.webapp.FacesServlet alias:/*.jsp(所有*.jsp文件都由FacesServlet处理)?
非常感谢,如果问题很傻,请原谅,因为我是一个新手,对于RCP插件、Jetty和JSF领域不熟悉。

3
我猜你已经解决了这个问题,否则你就不会询问JSF和相关问题了。另外,如果你使用JSF 2,那么你应该转向Facelets。我建议你阅读什么是JSP和Facelets之间的区别?JSF、Servlet和JSP之间的区别是什么?以及其中的链接。请不要误解我的意思,我并不反对你,只是为了提供一些更好的指引而已。 - Luiggi Mendoza
另外,如果您有任何问题的答案,可以回答并接受它们以帮助其他人获取指导。 - Luiggi Mendoza
非常感谢您的建议!您猜得很准。我已经解决了这个问题,但是我没有时间写一个适当的答案。不过很快我会回来带着答案。 - wallE
3
@wallE,你准备好写回答了吗? - sharakan
1
你尝试过访问localhost:8080/吗?你是否将war文件复制到了Jetty的war文件夹中?它可以通过终端或Maven [mvn jetty:run]运行Jetty时获取文件。 - A.P.S
2个回答

1

org.eclipse.equinox.jsp.jasper.registry中的JSP Extension Factory类提供了JSP支持,可以与servlet扩展点一起使用。

JSPFactory可与org.eclipse.equinox.http.registry和Servlets扩展点一起使用,以允许在扩展注册表中声明式地使用JSP。

JSPFactory将接受一个“路径”参数,对应于在包中查找JSP资源的基本路径。可以使用“:”分隔符方法或xml参数来设置此参数。

例如,class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/A/PATH"或


1

请查看在jetty中设置上下文。您可以在启动服务器之前定义它。

public class OneWebApp
{
    public static void main(String[] args) throws Exception
    {
        String jetty_home = System.getProperty("jetty.home","..");

        Server server = new Server(8080);

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setWar(jetty_home+"/webapps/test.war");
        server.setHandler(webapp);

        server.start();
        server.join();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接