这些天我正在努力学习JSF + Facelets.我有一个BackingBean和一个Facelet xHTML页面.当我请求facelet页面(只有一次)时,会多次调用backing-bean方法.
这可能是什么原因?
我看不到什么特别的东西.提前致谢.
这是facelet:
<?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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<ui:composition template="index.xhtml">
<ui:define name="content">
<h:form>Name: <h:inputText id="nameFilterPattern" value="#{kundenBackingBean.nameFilterPattern}" /><h:commandButton value="Suchen"/></h:form>
<h:dataTable var="kunde" value="#{kundenBackingBean.kunden}" rowClasses="rowHighlight, rowOrdinary">
<h:column>
<f:facet name="header">
<h:outputText value="Kundennr" />
</f:facet>
<h:outputText value="#{kunde.kundenNr}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{kunde.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Vorname" />
</f:facet>
<h:outputText value="#{kunde.vorname}"/>
</h:column> …Run Code Online (Sandbox Code Playgroud) 我有一个带有表单的jsf页面,其中包含一个outputtext.outputtext组件的值是从辅助bean(或托管bean)调用的.我知道当我将其编码为#{MyBean.myString}时,Jsf将其重命名并调用getMyString()方法.然而,奇怪的是,当我将断点放到此组件的getter方法时,我看到它在页面呈现期间被调用了两次.outputtext在ah:form中,它是唯一绑定到backingbean的组件.我的意思是,jsf在第一次进入getter方法时应该得到它是如此奇怪,但它需要两次进入getter方法.你能解释一下jsf中这种行为的原因是什么吗?
任何帮助将不胜感激,祝福,巴里斯
我在请求范围内有一个操作。其方法之一的返回值被传递到自定义 Facelet 标记。然后,该标记提取返回对象的多个属性并显示它们。问题在于,每次对返回对象的属性求值时,都会调用对 Action 进行方法调用的 EL 表达式。我将把相关的代码片段放在这里。
一些.xhtml
<ui:include src="someOther.xhtml">
<ui:param name="profileUri" value="#{param['relateToProfile']}"/>
<ui:param name="qualifier" value="#{param['qualifier']}"/>
<ui:param name="cellStyleClass" value="#{param['cellStyle']}"/>
</ui:include>
Run Code Online (Sandbox Code Playgroud)
someOther.xhtml(方法1)注意ProfileAction位于@RequestScoped中
<tenui:entityCard profileEntity="#{profileAction.getProfileMetadata(profileUri)}"
qualifier="#{qualifier}"
cellStyleClass="#{cellStyleClass}"/>
Run Code Online (Sandbox Code Playgroud)
enityCard.xhtml(facelet自定义标签)
<ui:fragment rendered="#{profileEntity.featured}">...
<tenui:gridCell id="#{profileEntity.profileId}#{qualifier}" ...
<tenui:metaunit ..content="#{profileEntity.getMeta('memberName')}"
href="/#{profileEntity.profileDisplayUri}"
hrefStyleClass="a-styled grid-cell-name"/>
.....
...several other EL expressions including #{profileEntity.xxx}
Run Code Online (Sandbox Code Playgroud)
问题是 #{profileAction.getProfileMetadata(profileUri)} 正在为entityCard.xhtml中的每个属性评估调用然后,我想我应该将方法调用的返回值保存在ac:set var(方法2,如下所述)中,但它没有帮助。
someOther.xhtml(方法 2)
<c:set var="profileMetadata"
value="#{profileAction.getProfileMetadata(profileUri)}"/>
<tenui:entityCard profileEntity="#{profielMetadata}"
qualifier="#{qualifier}"
cellStyleClass="#{cellStyleClass}"/>
Run Code Online (Sandbox Code Playgroud)
该操作方法调用一个相当昂贵的存储过程,并且返回的对象有超过 20 个属性,这些属性在entityCard.xhtml 中的 EL 中进行评估。
我还尝试了另一种方法,通过直接调用操作方法来解析 ui:param 本身的值,但没有任何效果。问题仍然存在。
有人能指出我可能做错了什么吗?或者,如何避免多次调用 profileAction.getProfileMetadata 调用?
我在那里读过,但我不能从primefaces datatable cellEditor获取编辑值,它给了我未经编辑的值.我正在使用jpa.xhtml页面:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<ui:composition 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"
xmlns:p="http://primefaces.prime.com.tr/ui"
template="/templates/masterLayout.xhtml">
<ui:define name="windowTitle">
learn
</ui:define>
<ui:define name="content">
<h:form>
<p:dataTable value="#{lesson.lessonValue}" var="l" style="width: 400px">
<p:ajax event="rowEdit" listener="#{lesson.onEditRow}"/>
<p:column headerText="Lessons" style="width: 300px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{l.lessonName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{l.lessonName}" style="width: 100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
lesson.java:
public class lesson implements Serializable {
private String name;
protected EntityLesson[] lessonList;
public String getName() { return name; } …Run Code Online (Sandbox Code Playgroud) 当我尝试渲染视图时,浏览器显示此错误
01:46:11,371 GRAVE [javax.enterprise.resource.webcontainer.jsf.application] (http--127.0.0.1-8080-1) Error Rendering View[/index.xhtml]: javax.el.PropertyNotFoundException: /index.xhtml @15,74 value="#{actividades.getAll}": The class 'org.pfc.controller.principal.ActividadesController' does not have the property 'getAll'.
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
Run Code Online (Sandbox Code Playgroud)
ActvidadController代码
@ManagedBean(name="actividades")
@ViewScoped public class ActividadesController implements Serializable {
private static final long serialVersionUID = 1L;
private final static Log logger=LogFactory.getLog(ActividadesController.class);
@ManagedProperty(value="#{actividadBO}")
private ActividadBO actividad;
public void setActividad(ActividadBO actividad) {
this.actividad = actividad;
}
public List<Actividad> getAll(){
logger.trace("ActividadesController.getAll");
return actividad.getAll();
}
}
Run Code Online (Sandbox Code Playgroud)
查看代码
<h:body>
<ui:composition template="/WEB-INF/templates/main-template.xhtml">
<ui:define name="content">
<h:dataTable value="#{actividades.getAll}" var="actividad">
<h:column>
<f:facet name="header">
<h:outputText>Título</h:outputText>
</f:facet> …Run Code Online (Sandbox Code Playgroud) 让我们有一些1.xhtml包含的facelet
<h:inputText id="prop" value="#{MyBean.myProperty}"/>
Run Code Online (Sandbox Code Playgroud)
和2.xhtml包含的facelet
<h:inputText id="prop" value="${MyBean.myProperty}"/>
Run Code Online (Sandbox Code Playgroud)
从官方教程引用:
Immediate evaluation means that the expression is evaluated and the result returned as soon as the page is first rendered.
Run Code Online (Sandbox Code Playgroud)
我不明白立即表达的具体阶段是什么?在Render Response阶段或Update model values或Apply request或什么?
我知道在getter和setter中编写业务逻辑是一种非常糟糕的编程习惯,但如果响应已经提交,有没有办法处理异常?
"响应已经提交"和"标题已经发送给客户"的含义究竟是什么?
我正在使用JSF和PrimeFaces,但我需要获取组件id的值.因为我正在构建具有不同id的dinamycally面板,以显示我需要比较的面板,如果是当前面板,则显示它.
例如,如果我有下一个面板
<p:outputPanel id="#{bean.getID}" autoUpdate="true"
renderer=#{@this.id == bean.currentPanel}
>
</p:outputPanel>
Run Code Online (Sandbox Code Playgroud)
和豆
public class Bean(){
private int numberPanels =0;
private int currentPanel = 0;
public int getID(){
//...a process that return different ID
}
// getter's and setters
Run Code Online (Sandbox Code Playgroud)
}
显然,@this.id不起作用.那么,如何使用PrimeFaces获取componente ne JSF的ID值?
我正在使用<p:dataTable>行编辑器编辑数据,如下所示.
<p:dataTable value="#{bean.users}" var="user" editable="true">
<p:ajax event="rowEdit" listener="#{bean.onRowEdit}" />
<p:ajax event="rowEditCancel" listener="#{bean.onRowEditCancel}" />
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{user.firstName}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{user.firstName}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
支持bean的实现如下.
private List<User> users;
@EJB
private UserService userService;
public List<User> getUsers() {
users = userService.list();
return users;
}
Run Code Online (Sandbox Code Playgroud)
当我在cellEditor中输入新数据并提交它时,它们在侦听器方法中不可用.我注意到它们被数据库调用的数据覆盖了.
为什么会发生这种情况,我该如何避免呢?
我目前的环境是JRE 1.7,JSF 2.2,Eclipse Luna.在entity_index.xhtml我的应用程序的某个页面()中,我有以下(PrimeFaces)按钮:
<p:commandButton value="Details" action="entity_details"
ajax="false" onclick="this.form.target='_blank'">
<f:param name="id" value="#{entity.id}" />
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)
我们的想法是提供一个按钮,以便用户可以单击它,当前实体的一些细节将显示在另一个浏览器选项卡(页面entity_details.xhtml)中.这是许多的一个按钮,因此entity_index.xhtml页面显示实体的许多实例,每个实例都有一个详细信息按钮.
按钮的工作方式是新选项卡打开并显示正确的页面(entity_details.xhtml),但实体ID永远不会到达处理详细信息页面的bean(EntityDetailsMB).详情页面如下:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:dsi="http://www.cce.ufpr.br"
template="/private/template/sbadmin.xhtml">
<f:metadata>
<f:viewParam name="id" value="#{entityDetailsMB.id}"/>
</f:metadata>
<ui:define name="content">
<h2 class="page-header">#{entityDetailsMB.entity.name}</h2>
<h:form id="form">
...
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
请注意,有一个<f:metadata/>元素专门用于捕获从索引页面发送的参数并将其转发到id属性中EntityDetailsMB,其中包含以下内容:
public Entity getEntity() {
return entityById(id);
}
public Long getId() {
return id;
}
public void setId(Long value) {
id = value;
}
Run Code Online (Sandbox Code Playgroud)
因为setId()从不调用该方法,所以 …