JSF动态加载几乎工作 - 但没有CommandButton控件控件、加载、动态、工作

2023-09-10 20:07:11 作者:枕上诗书闲

我要动态地包括在运行时.xhtml的文件,你可以称之为 JSF的前端,以在实体层面的继承层次。我期望 有一个总的100×30-50线的xhtml文件将仅装 如果需要正确处理的基础数据。一个典型的用例是 那一个或两个的xhtml文件将需要工作在基础数据, 所以你可以想像,我不希望每次加载完整100,它会 是愚蠢的刑事责任这样做的。

现在我知道,有无穷无尽的讨论,在那里对这个话题,但 无数个小时的研究后,我已经得到了precisely行不通的。我在用 Mojarra 2.1.1。

在下面我最初用户界面的例子:有一个'空'的文件,一个文件 (而不是C:抓)最小的内容,然后单击命令按钮其中 改变将包括在内容new.xhtml并更新容器 UI的:包括与阿贾克斯

在new.xhtml的内容显示在页面上,所以这看起来不错。该 问题是,我有在new.xhtml一个命令,而当这个文件 是动态包含命令按钮将无法正常工作 - 操作例程 永远不会被调用。如果您使用的用户界面:包括new.xhtml(而不是#{bean.page}) 它工作正常。于是几个问题:

在有其他人得到了这个工作? 如果不是有这样做的另一种方式? 为什么事情如此令人难以置信的明显的和有用的这么难呢?

我不得不使用jQuery负载()今天发挥,而我可以将文件加载使用它 有各种问题。这些文件我想加载包含PrimeFaces 对话时的jQuery加载的对话似乎短暂闪烁起来 上消失,永远的深渊前的画​​面。其他报告 组件没有任何CSS样式。

感谢。

编辑:我应该说清楚,我的愿望,做动态负载,而不是       简单地导航到不同的页面,因为我只是想用       根据底层的类型primefaces对话框,所不同的       数据。这将是这样做的一个更合适的方法,如果可能的话。

的index.xhtml

 < XML版本=1.0编码=UTF-8&GT?;
!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml
      的xmlns:H =htt​​p://java.sun.com/jsf/html
      的xmlns:UI =htt​​p://java.sun.com/jsf/facelets
      的xmlns:F =htt​​p://java.sun.com/jsf/core>
    < H:头>
        <冠军>的facelet标题< /标题>
    < / H:头>
    < H:身体GT;
        < H:形式GT;
            < H:的commandButton值=推的动作=#{bean.doit}>
                < F:AJAX渲染=装载/>
            < / H:的commandButton>
            < BR />
            < H:panelGroup中的id =负荷>
                <用户界面:包括SRC =#{bean.page}/>
                < UI:删除>
                <用户界面:包括SRC =new.xhtml/>
                < / UI:删除>
            < /小时:panelGroup中>
        < /小时:形式GT;
    < / H:身体GT;
< / HTML>
 
VB中窗体能像commandbutton一样建立窗体组吗

new.xhtml

 < XML版本=1.0编码=UTF-8&GT?;
!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0 Strict标准// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml
                的xmlns:UI =htt​​p://java.sun.com/jsf/facelets
                的xmlns:H =htt​​p://java.sun.com/jsf/html
                的xmlns:F =htt​​p://java.sun.com/jsf/core>
    < H:的outputText值=new.xhtml/>
    < BR />
    < H:的commandButton值=增量行动=#{bean.inc}>
        < F:AJAX />
    < / H:的commandButton>
< / HTML>
 

empty.xhtml

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml/>
 

Bean.java

 进口javax.faces.bean.ManagedBean;
进口javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
公共类豆{

    私人字符串页=/empty.xhtml;

    公共字符串DOIT(){
        页=/new.xhtml;
        返回null;
    }
    //干将和放大器; setter方法​​去除
}
 

解决方案

这个问题原来是该用户界面:包括由标签处理程序实现 这是在执行恢复视图,而不是调用过程中重新执行 应用程序,所以当它改变了组件树应用程序逻辑 不知道的改变。只有在使用部分状态这是真的 节能。

我复制从UI的code:包括标签处理成一个非常简单的自定义 分量和至少可以让采取行动的方法来火,但并不可靠。

解决的办法是排除的意见,要求此动态UI:包括功能 从使用部分状态保存在web.xml中的条目是这样的:

 <的context-param>
  <参数 - 名称>&javax.faces.FULL_STATE_SAVING_VIEW_IDS LT; /参数 - 名称>
  &LT;参数值&GT; /index.xhtml< /参数值&GT;
&LT; /的context-param&GT;
 

这已经过测试,在多个页面包含多个CommandButton控件 片段,似乎可靠地工作(与ViewScoped JSF豆测试)。我已经 还包括动态包含primefaces对话框的文件,这工作 确定为好。我希望这是一些使用给别人当它采取了一个好大块 一次我的日程安排,但至少它现在的工作,这是很好的。

干杯。

I want to dynamically include .xhtml files at runtime, what you might call the JSF front end to an inheritance hierarchy at the entity level. I expect to have a total of 100 x 30-50 line xhtml files that would be loaded only if required to correctly handle the underlying data. A typical use case is that one or two xhtml files would be required to work on the underlying data, so you can imagine that I don't want to load the full 100 every time, it would be criminally stupid to do so.

Now I know that there are endless discussions out there on this topic but after countless hours of research I have got precisely nowhere. I am using Mojarra 2.1.1.

In the example below I initially ui:include an 'empty' file, a file with minimal content (instead of c:catch), then click a commandButton which changes the content to be included to new.xhtml and updates the container of the ui:include with ajax.

The content of the new.xhtml is shown on the page, so this looks good. The problem is that I've got a commandButton in new.xhtml, and when this file is dynamically included the commandButton will not work - the action routine is never called. If you use ui:include new.xhtml (instead of #{bean.page}) it works fine. So a few questions:

Has anyone else got this working? If not is there another way of doing it? Why is something so incredibly obvious and useful so hard to do?

I had a play with jQuery load() today, and whilst I can load files using it there are various issues. The files I want to load will contain PrimeFaces dialogs and when loaded by jQuery the dialogs seemed to flash up momentarily on the screen before disappearing, forever, into the abyss. Others report components not having any css styling.

Thanks.

Edit: I should make it clear that my desire to do dynamic load rather than simply navigating to a different page is because I just want to use primefaces dialogs, a different one according to the type of underlying data. It would be a neater way of doing it, if it's possible.

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton value="push" action="#{bean.doit}">
                <f:ajax render="load"/>
            </h:commandButton>
            <br/>
            <h:panelGroup id="load">
                <ui:include src="#{bean.page}"/>
                <ui:remove>
                <ui:include src="new.xhtml"/>
                </ui:remove>
            </h:panelGroup>
        </h:form>
    </h:body>
</html>

new.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <h:outputText value="new.xhtml"/>
    <br/>
    <h:commandButton value="inc" action="#{bean.inc}">
        <f:ajax/>
    </h:commandButton>
</html>

empty.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"/>

Bean.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Bean {

    private String page = "/empty.xhtml";

    public String doit() {
        page = "/new.xhtml";
        return null;
    }
    // getters & setters removed
}

解决方案

The issue turns out to be that ui:include is implemented by a tag handler which is executed during restore view and not re-executed during invoke application, so when it modifies the component tree your application logic is not aware of the changes. This is true only when using partial state saving.

I copied the code from the ui:include tag handler into a very simple custom component and could at least get action methods to fire, but not reliably.

The solution is to exclude views requiring this dynamic ui:include functionality from using partial state saving with an entry in web.xml like this:

<context-param>
  <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
  <param-value>/index.xhtml</param-value>
</context-param>

This has been tested with multiple commandButtons in multiple included page fragments and seems to work reliably (tested with ViewScoped JSF beans). I've also dynamically included a file containing a primefaces dialog and this works ok as well. I hope this is of some use to others as it's taken a good chunk of time out of my schedule, but at least it's working now, which is good.

Cheers.