简体   繁体   English

JSF2和自定义组件-如何从Javascript代码调用动作?

[英]JSF2 and custom components - how to call action from Javascript code?

I have adapted a Pie chart JS object to use with JSF; 我已经修改了饼图JS对象以与JSF一起使用; in order to do so, I've created a custom component that outputs the appropriate markup and JS calls on the page, rendering the graph correctly. 为了做到这一点,我创建了一个自定义组件,该组件在页面上输出适当的标记和JS调用,以正确呈现图形。 For reference's sake, this is the link for the tutorial I've followed for the JS part. 为了参考, 这是我遵循的JS部分教程的链接

The next step is listening to clicks on the slices, and calling an action from a backing bean. 下一步是侦听切片上的点击,并从支持Bean调用操作。 The JS object for the chart already contains a placeholder function that listens to such clicks, so I believe the JS part of it is good. 图表的JS对象已经包含一个占位符函数,可监听此类点击,因此我认为其中的JS部分很好。 However, the JSF side bugs me still; 但是,JSF方面仍然困扰着我。 I've read the Java EE tutorial, Jim Driscoll's blog posts, and all over the internet, and still can't get my head around it. 我已经阅读了Java EE教程,Jim Driscoll的博客文章以及遍及整个Internet的内容,但仍然无法解决。

So, could anyone be so kind as to give a little example, of how I could bind a JS function call to an event listener in JSF, so that my backing bean would be nicely informed of which slice index had been clicked by the user? 因此,有谁能举一个例子说明我如何将JS函数调用绑定到JSF中的事件侦听器,以便我的后备Bean能够很好地了解用户单击了哪个切片索引?

It would be something close to: 它将接近:

function myChartObject() {

    function onSliceClick() {
        // This will somehow trigger JSF ajax event listener with slice data
    }
}

class MyCustomChart extends UIComponentBase implements ClientBehaviorHolder {
    // Is the decode() method the place to bind JS calls to JSF actions?
}

The closest I've found to my problem is something like this . 我发现我的问题最接近的是像这样 However, I'd like to have this support in my own component, using the standard JSF API. 但是,我希望使用标准JSF API在自己的组件中提供此支持。 Something perhaps close to this ? 也许接近这个

Thank you all in advance! 谢谢大家!

The JSF Javascript API to do AJAX calls is itself standardized (resource library "javax.faces", resource name "jsf.js") but adding full AJAX support in your own Java based custom component is a little elaborate. 进行AJAX调用的JSF Javascript API本身是标准化的(资源库“ javax.faces”,资源名称“ jsf.js”),但是在您自己的基于Java的自定义组件中添加完整的AJAX支持则有些复杂。

The quickest way I can think of is following the blog by Jim Driscoll that you cited, and re-use the existing AJAX machinery offered by the <f:ajax> tag by wrapping your own Java based custom component in a composite component. 我能想到的最快的方法是跟随您引用的Jim Driscoll的博客,并通过将您自己的基于Java的自定义组件包装在复合组件中来重用<f:ajax>标记提供的现有AJAX机制。

In Jim's example, I guess the following code from line 22 in his example is what you should render inside your onSliceClick function: 在Jim的示例中,我想他示例中第22行的以下代码是应在onSliceClick函数内部呈现的onSliceClick

String click = behaviors.get("click").get(0).getScript(behaviorContext);

It would then look a little like this: 然后看起来像这样:

<ui:component
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:cu="http://javaserverfaces.dev.java.net/demo/custom-taglib" 
>
    <cc:interface shortDescription="Some Description">
        <cc:attribute name="render" required="false" />     
        <cc:attribute name="clickAction" method-signature="java.lang.Object action" required="true" shortDescription="The click action method" />
    </cc:interface>
    <cc:implementation>
        <cu:custom id="customId">
            <f:ajax render="#{cc.attrs.render}" listener="#{cc.attrs.clickAction}"/>
        </cu:custom>
    </cc:implementation>
</ui:component>

Note that I haven't tested this, but it's the general idea. 请注意,我尚未对此进行测试,但这是一般的想法。 Of course it's also possible to do all of it directly in Java code but that surely takes some more work. 当然,也可以直接在Java代码中完成所有这些操作,但这肯定需要更多的工作。

It looks like you want the ability to encode ajax style behaviors in your java compiled code, while integrating this with a J2ee stack. 看起来您希望能够在Java编译代码中对Ajax样式行为进行编码,同时将其与J2ee堆栈集成。

The framework that comes to mind is JBOSS's Seam 想到的框架是JBOSS的Seam

  1. Seam integrates directly with JSF by design, (GWT is more of a lightweight, standalone, statefull web-application framework - it doesn't have a lot of embedded features for direct integration with JMS and other modern J2ee features)... Seam通过设计直接与JSF集成(GWT更像是一个轻量级的,独立的,有状态的Web应用程序框架-它没有很多与JMS和其他现代J2ee功能直接集成的嵌入式功能)...

  2. Seam directly supports GWT-style ajax enabled components. Seam直接支持启用GWT风格的Ajax组件。

There is a good tutorial here : http://www.ibm.com/developerworks/java/library/j-seam3/ 这里有一个很好的教程: http : //www.ibm.com/developerworks/java/library/j-seam3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM