数据库中的JSF2 Bean的一些数据在渲染时不可用(Some data of JSF2 Bean from database not available at render time)
渲染JSF2(Facelet)页面时,我遇到了一个奇怪的问题。 通常是通过
GET
接收id
并显示对象的页面。 该对象内部有一个List <>,问题是有时该列表不打印任何内容,有时我刷新并部分打印列表(所有元素但不是所有关于它们的信息)。 它也与其他对象的属性(某些日期)一起发生。 我已经检查了一些日志记录,并且从数据库和对象set
正确获取了信息。我很确定这是因为
preRenderView
是在渲染之前完成的,所以当我使用c:if
或c:each
时,bean很可能无法使用。 对于第二种情况,也许ui:repeat
会解决我的问题 ?我的问题是:
- 我怎样才能解决这个问题?
- 在Facelets中有没有办法渲染f.ex.
<section>
或<time>
(如下面的document.xhtml
所示)如果渲染计算为false,则不打印空标记? 我知道我可以使用c:if
,但在Facelets中推荐使用。- 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
viaGET
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 objectset
s 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 usec:if
orc:each
. For the second case, perhapsui:repeat
would solve my problem?.My questions are:
- How can I fix this?
- Is there a way in Facelets, to render f.ex. a
<section>
or<time>
(as in mydocument.xhtml
below) and don't print the empty tag if rendered computes to false? I know I can usec:if
, but rendered is recommended in Facelets.- Has the DB Javabean
Document
to beNamed
as well (besides DocumentController)?Also, please, if there is a better way to do what I am doing (a page that receives an
id
viaGET
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
最满意答案
我是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 for .net 4.0 / WebApi 1的替代方案(Alternative for StringContent for .net 4.0 / WebApi 1)[2023-08-17]
奇怪的是,当搜索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.
-
.NET 4.0上的OpenX API(OpenX API on .NET 4.0)[2022-03-11]
我是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 ...