首页 \ 问答 \ 数据库中的JSF2 Bean的一些数据在渲染时不可用(Some data of JSF2 Bean from database not available at render time)

数据库中的JSF2 Bean的一些数据在渲染时不可用(Some data of JSF2 Bean from database not available at render time)

渲染JSF2(Facelet)页面时,我遇到了一个奇怪的问题。 通常是通过GET接收id并显示对象的页面。 该对象内部有一个List <>,问题是有时该列表不打印任何内容,有时我刷新并部分打印列表(所有元素但不是所有关于它们的信息)。 它也与其他对象的属性(某些日期)一起发生。 我已经检查了一些日志记录,并且从数据库和对象set正确获取了信息。

我很确定这是因为preRenderView是在渲染之前完成的,所以当我使用c:ifc:each时,bean很可能无法使用。 对于第二种情况,也许ui:repeat 会解决我的问题

我的问题是:

  1. 我怎样才能解决这个问题?
  2. 在Facelets中有没有办法渲染f.ex. <section><time> (如下面的document.xhtml所示)如果渲染计算为false,则不打印空标记? 我知道我可以使用c:if ,但在Facelets中推荐使用。
  3. DB Javabean Document是否也被Named (除了DocumentController)?

另外,如果有更好的方法来做我正在做的事情(通过GET接收id并显示对象的页面),请告知。 我是JSF的新手。

顺便说一句,我已经丢弃它是由于这个问题

DocumentController.java

@Named(value = "DocumentController")
@SessionScoped
public class DocumentController implements Serializable {
    private String id;
    private Document document;

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the Document
     */
    public Document getDocument() {
        return document;
    }

    /**
     * @param Document the Document to set
     */
    public void setDocument(Document document) {
        this.document = document;
    }


    public void load() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (ctx.isValidationFailed()) {
            ctx.getApplication().getNavigationHandler()
                    .handleNavigation(ctx, "#{DocumentController.load}", "invalid");
            return;
        }

        try (DataStore dst = new DataStore()) {
            dst.connect();
            document = dst.getDocument(id);

        } catch (NoData | ConnectionError | IllegalArgumentException ex) {
            ctx.getApplication().getNavigationHandler()
                    .handleNavigation(ctx, "#{DocumentController.load}", "invalid");
        }
    }

}

document.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:utils="http://java.sun.com/jsf/composite/utils"
                template="template.xhtml">
    <ui:define name="content">
        <f:metadata>
            <f:viewParam name="id" value="#{documentController.id}" required="true"/>
            <f:event type="preRenderView" listener="#{documentController.load}"/>
        </f:metadata>
...
        <section rendered="#{not empty documentController.document.participants}">
            <utils:participants
                participants="#{documentController.document.participants}
                cid="example"/>
....
    </ui:define>
</ui:composition>

participants.xhtml

<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:cc="http://xmlns.jcp.org/jsf/composite"
              xmlns:c="http://java.sun.com/jsp/jstl/core"
              xmlns:ui="http://java.sun.com/jsf/facelets">
    <cc:interface>
        <cc:attribute name="participants" type="java.util.List" required="true"/>
        <cc:attribute name="cid" type="String" required="true"/>
    </cc:interface>

    <cc:implementation>
        <table>
...
            <tbody>
                <c:forEach items="#{cc.attrs.participants}" var="participant">
                    <tr>
                        <td><a href="#">#{participant.name}</a></td>
                        <td>#{participant.lastName}</td>
                        <td>#{participant.role(cc.attrs.cid)}</td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </cc:implementation>
</ui:component>

I am running into a weird problem when rendering JSF2 (Facelet) pages. It is the usual page that receives an id via GET and displays the object. The object has a List<> inside, and the problem is that sometimes that list prints nothing and sometimes I refresh and prints the list partially (all the elements but not all the information about them). It also happens with other object's attributes (some dates). I have checked with some logging and the information is obtained correctly from the DB and the object sets the information.

I am quite sure that this is because preRenderView is done just before the render, so the bean is by chance not available when I use c:if or c:each. For the second case, perhaps ui:repeat would solve my problem?.

My questions are:

  1. How can I fix this?
  2. Is there a way in Facelets, to render f.ex. a <section> or <time> (as in my document.xhtml below) and don't print the empty tag if rendered computes to false? I know I can use c:if, but rendered is recommended in Facelets.
  3. Has the DB Javabean Document to be Named as well (besides DocumentController)?

Also, please, if there is a better way to do what I am doing (a page that receives an id via GET and displays the object), please advise. I am totally new to JSF.

Btw, I have discarded it is due to this problem.

DocumentController.java

@Named(value = "DocumentController")
@SessionScoped
public class DocumentController implements Serializable {
    private String id;
    private Document document;

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the Document
     */
    public Document getDocument() {
        return document;
    }

    /**
     * @param Document the Document to set
     */
    public void setDocument(Document document) {
        this.document = document;
    }


    public void load() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (ctx.isValidationFailed()) {
            ctx.getApplication().getNavigationHandler()
                    .handleNavigation(ctx, "#{DocumentController.load}", "invalid");
            return;
        }

        try (DataStore dst = new DataStore()) {
            dst.connect();
            document = dst.getDocument(id);

        } catch (NoData | ConnectionError | IllegalArgumentException ex) {
            ctx.getApplication().getNavigationHandler()
                    .handleNavigation(ctx, "#{DocumentController.load}", "invalid");
        }
    }

}

document.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:utils="http://java.sun.com/jsf/composite/utils"
                template="template.xhtml">
    <ui:define name="content">
        <f:metadata>
            <f:viewParam name="id" value="#{documentController.id}" required="true"/>
            <f:event type="preRenderView" listener="#{documentController.load}"/>
        </f:metadata>
...
        <section rendered="#{not empty documentController.document.participants}">
            <utils:participants
                participants="#{documentController.document.participants}
                cid="example"/>
....
    </ui:define>
</ui:composition>

participants.xhtml

<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:cc="http://xmlns.jcp.org/jsf/composite"
              xmlns:c="http://java.sun.com/jsp/jstl/core"
              xmlns:ui="http://java.sun.com/jsf/facelets">
    <cc:interface>
        <cc:attribute name="participants" type="java.util.List" required="true"/>
        <cc:attribute name="cid" type="String" required="true"/>
    </cc:interface>

    <cc:implementation>
        <table>
...
            <tbody>
                <c:forEach items="#{cc.attrs.participants}" var="participant">
                    <tr>
                        <td><a href="#">#{participant.name}</a></td>
                        <td>#{participant.lastName}</td>
                        <td>#{participant.role(cc.attrs.cid)}</td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </cc:implementation>
</ui:component>

原文:https://stackoverflow.com/questions/36457213
更新时间:2023-09-07 18:09

最满意答案

我是OpenX.NET的作者。 如果您只需要在Web应用程序中提供OpenX广告,则无需使用这些API绑定。 只需将OpenX javascript放入您的网页即可。

如果您确实需要将API用于其他类型的场景并且是OpenX的新手,我建议您首先熟悉OpenX概念,请参阅OpenX开发人员专区 。 然后看一些OpenX.NET测试 。 OpenX.NET是OpenX实体和方法的一对一映射。

编辑:除非你想调试一些东西或者真的知道你在做什么,否则你永远不需要直接处理代理。 只需使用会话构造函数:

SessionImpl(string username, string password, string url)

URL是OpenX v2 API的根URL,例如http://localhost:10002/openx/api/v2/xmlrpc/ ,请参阅测试中示例app.config


I'm the author of OpenX.NET. If you just need to serve OpenX ads in a web application, you don't need to use these API bindings. Just place the OpenX javascript in your web page.

If you really need to use the API for other kind of scenarios and are new to OpenX, I recommend first getting familiar with OpenX concepts, see the OpenX Developer Zone. Then see some OpenX.NET tests. OpenX.NET is a 1-to-1 mapping to OpenX entities and methods.

EDIT: unless you want to debug something or really know what you're doing, you never need to handle the proxy directly. Simply use the session constructor:

SessionImpl(string username, string password, string url)

The URL is the root URL for the OpenX v2 API, e.g. http://localhost:10002/openx/api/v2/xmlrpc/, see the sample app.config in the tests.

相关问答

更多
  • 奇怪的是,当搜索StringContent类时,它会进入msdn页面,说它在.net 4.5中 由于该项目之前的目标是.net 4.5,我认为这是我的intellisense。 然而,在引用之后它似乎是在我的nuget中组装并且路径表明它在.net 4.0中没问题。 程序集System.Net.Http.dll,v2.0.0.0 \ packages \ Microsoft.Net.Http.2.0.20710.0 \ lib \ net40 \ System.Net.Http.dll Weirdly ev ...
  • 针对.NET 4.0框架的应用程序代码不能在仅安装.NET 2.0框架的环境中运行。 请参阅此处的“版本兼容性”部分: http : //msdn.microsoft.com/en-us/library/8477k21c.aspx Application code targeted against the .NET 4.0 framework will not run in an environment that only has the .NET 2.0 framework installed. See ...
  • 使用ThreadPool和IIS的任何警告仍然有效,使用System.Threading.Task ,因为Task API只是System.Threading一个额外的抽象层。 对于后台长时间运行的任务,我在Windows服务中使用ThreadPool 。 这使它保持在IIS之外。 Any caveats to using ThreadPool with IIS would still be valid using System.Threading.Task, as the Task API is just ...
  • 回答我自己的问题: 不,不可能用.net 4.0使用Web API 2: 这是由达米安爱德华兹的幻灯片。 您可以在这里找到附加信息。 To answer my own question: No, it is NOT possible to use Web API 2 with .net 4.0: This is taken from a slide by Damian Edwards. You can find additinal info here.
  • 我们故意选择不支持.NET 4.0 for Web API,因为WebApiRequestLifestyle使用CallContext.LogicalGetData ,它在.NET 4.0下行为不同。 这种行为显着不同,当在后台线程和并行运行的Tasks中使用嵌套的ExecutionContextScope实例时,它可能会导致错误。 在这方面的变化是,在.NET 4.5中,逻辑调用上下文展现了写时复制行为 ,这意味着当逻辑调用上下文从并行操作中改变时,它不会影响具有产卵的原始操作这个并行操作。 在.NET ...
  • .NET 3.5和.NET 4之间的安全模型发生了巨大的变化。 http://msdn.microsoft.com/en-us/library/dd233103.aspx The security model changed pretty dramatically between .NET 3.5 and .NET 4. http://msdn.microsoft.com/en-us/library/dd233103.aspx
  • 尝试检查您引用的dll是否适用于版本4.0而不是4.5,您可能仍在引用4.5库,这些库不适用于.net 4。 Try to check that the dlls you are referencing are for version 4.0 and not 4.5, could be you are still referencing 4.5 libraries which won't work with .net 4.
  • 我是OpenX.NET的作者。 如果您只需要在Web应用程序中提供OpenX广告,则无需使用这些API绑定。 只需将OpenX javascript放入您的网页即可。 如果您确实需要将API用于其他类型的场景并且是OpenX的新手,我建议您首先熟悉OpenX概念,请参阅OpenX开发人员专区 。 然后看一些OpenX.NET测试 。 OpenX.NET是OpenX实体和方法的一对一映射。 编辑:除非你想调试一些东西或者真的知道你在做什么,否则你永远不需要直接处理代理。 只需使用会话构造函数: Session ...
  • 我发现charanjit singh这个简单的解决方案。 它工作得很好,特别是如果你遇到旧的visual studio 2010,.Net 4.0,当然还有web api 1.基本上将这个功能添加到你的Global.asax.cs protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Ori ...
  • 显然没有这样的方法。 通过添加?overload = medium解决了这个问题,它将从OpenX中获取所有站点,然后在我的客户端中迭代遍历站点列表并比较account_id。 There is no such method apparently. Solved it by adding ?overload=medium, which will fetch all sites from OpenX and then in my client iterate through list of sites and ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)