且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何动态地创建表单控件使用DHTML和处理所有与JSF?

更新时间:2022-04-27 02:44:59

这是可能的,但你需要有JSF API为不完全​​微不足道的一个比较好的了解。做到这一点的方法是在充当一个容器,您将创建客户端上的动态HTML组件树一个基于Java的自定义组件。

This is possible, but you need to have a relatively good understanding of the JSF API as its not exactly trivial. The way to do it is to have a Java based custom component in the component tree that serves as a container for the dynamic HTML you will be creating on the client.

通过任何 UIComponent#德code 渲染#德code (如果你使用一个独立的渲染器),您的Java自定义组件可以在回发后检查请求并填充本身具有原始值动态创建的组件上传到服务器。

Via either UIComponent#decode or Renderer#decode (if you use a separate renderer) your Java custom component can inspect the request after a postback and populate itself with the raw values that the dynamically created components post to the server.

一个很好的方式得到一个想法是如何工作就是检查现有的渲染器。例如。从CheckboxRenderer一个简化的例子:

A good way to get an idea how this works is just to inspect existing renderers. E.g. a simplified example from the CheckboxRenderer:

public void decode(FacesContext context, UIComponent component) {

    clientId = component.getClientId(context);

    // Convert the new value
    Map<String, String> requestParameterMap = context.getExternalContext().getRequestParameterMap();
    boolean isChecked = isChecked(requestParameterMap.get(clientId));
    setSubmittedValue(component, isChecked);
}

private static boolean isChecked(String value) {

    return "on".equalsIgnoreCase(value)
           || "yes".equalsIgnoreCase(value)
           || "true".equalsIgnoreCase(value);

}

里面去code方法你处理所有的虚拟组件和JSF然后看见的一切,一个组成部分。

Inside the decode method you're handling all your 'virtual components' and JSF then sees everything as one component.

它也或多或少可以动态地创建已在客户端上被动态创建的组件的一个服务器侧重新presentation。我说或多或少因为Mojarra(JSF的参考实现)目前有一些错误的prevent这个工作,以及在所有情况。例如参见 http://java.net/jira/browse/JAVASERVERFACES-1826 的MyFaces似乎工作正确虽然。

It's also more or less possible to dynamically create a server side representation of the components that have been dynamically created on the client. I say more or less since Mojarra (the JSF reference implementation) currently has some bugs that prevent this working well in all situations. See e.g. http://java.net/jira/browse/JAVASERVERFACES-1826 MyFaces seems to work correctly though.

您可能会在理查德的Metawidget所做的工作特别感兴趣: http://metawidget.org/

You might be specifically interested in the work done by Richard for Metawidget: http://metawidget.org/

此外,我已经打开了一个问题,让这个充满活力的(服务器端)创建组件添加到JSF规范。如果你认为这对你的使用情况很重要,请给它投票:的http:// java的.NET / JIRA /浏览/ JAVASERVERFACES_SPEC_PUBLIC-1007

Furthermore, I've opened an issue to have this dynamic (server side) component creation added to the JSF spec. If you think this is important for your use case, please give it a vote at: http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1007