我再次看到@PostConstruct每次都在触发,即使没有使用绑定属性.看到这段代码: -
<?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:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<c:forEach var="item" items="#{TestBean.listItems}">
<h:outputText value="#{item}"/>
</c:forEach>
<h:commandButton value="Click" actionListener="#{TestBean.actionListener}"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是JSF中最简单的bean: -
package managedBeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name="TestBean")
@ViewScoped
public class TestBean implements Serializable {
private List<String> listItems;
public List<String> getListItems() {
return listItems;
}
public void setListItems(List<String> listItems) {
this.listItems = listItems;
} …Run Code Online (Sandbox Code Playgroud) 似乎今天(2012年4月),MyFaces和Mojarra的JSF 2.1实现都存在部分状态保存的缺陷,并且PARTIAL_STATE_SAVING应设置为false.
这是真的?
我在webapp中的几个地方有以下构造,以便根据某些操作有条件地呈现页面片段:
<h:panelGroup rendered="#{managedBean.serviceSelected == 'insurance'}">
<ui:include src="/pages/edocket/include/service1.xhtml" />
</h:panelGroup>
Run Code Online (Sandbox Code Playgroud)
我观察到,<ui:include>即使rendered属性评估,仍然会执行false.这会不必要地创建与包含的service1.xhtml文件关联的所有后备bean .
如何<ui:include>在不呈现父UI组件时跳过执行,以便不会不必要地创建所有这些支持bean?
我有一个问题是将h:selectBooleanCheckbox的列表绑定到我的bean.有人帮忙吗?
这不起作用:
<ui:repeat value="#{cartBean.productsList}" var="cartProduct" varStatus="i">
<h:selectBooleanCheckbox binding="#{cartBean.checkboxes[i.index]}" />
</ui:repeat>
public class CartBean extends BaseBean {
public List<Product> getProductsList() {...}
private HtmlSelectBooleanCheckbox[] checkboxes;
public HtmlSelectBooleanCheckbox[] getCheckboxes() {
return checkboxes;
}
public void setCheckboxes(HtmlSelectBooleanCheckbox[] checkboxes) {
this.checkboxes = checkboxes;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
javax.faces.FacesException: javax.el.PropertyNotFoundException: /WEB-INF/flows/main/cart.xhtml @26,97 binding="#{cartBean.checkboxes[i.index]}": Target Unreachable, 'checkboxes' returned null
Run Code Online (Sandbox Code Playgroud)
我解决了我的问题.我使用下面的代码得到我想要的东西(感谢BalusC博客 - http://balusc.blogspot.com/2006/06/using-datatables.html#SelectMultipleRows):
<ui:repeat value="#{cartBean.productsList}" var="cartProduct" varStatus="i">
<h:selectBooleanCheckbox value="#{cartBean.selectedIds[cartProduct.id]}" />
</ui:repeat>
public class CartBean extends BaseBean {
private Map<Integer, Boolean> selectedIds = new HashMap<Integer, Boolean>();
public Map<Integer, Boolean> …Run Code Online (Sandbox Code Playgroud) 我正在用jsf 2.0做一个页面,我想做这样的事情:
<f:metadata>
<f:viewParam name="id" value="${id}" />
<f:event type="preRenderView" listener="#{controller.initPage(id)}"/>
</f:metadata>
....(Some code)....
<c:forEach items="#{bean.listLoadedByInitPage}" var="var">
#{var.something}
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
initPage(id)方法必须在bean中加载列表.但似乎该方法是在c:forEach之后调用而不是之前调用的.有任何想法吗?
我想根据条件更改变量值,所以我尝试了以下内容:
<h:head>
<ui:param name="userCase" value="Insert" />
<ui:fragment rendered="#{employee.employeesBulkInsert==false}">
<ui:param name="userCase" value="Update" />
</ui:fragment>
<title>#{userCase} Employee </title>
</h:head>
Run Code Online (Sandbox Code Playgroud)
但它在更新的情况下不起作用,它显示一个空字符串,任何想法为什么?
我知道还有其他解决方案,比如在支持bean中定义变量,或者直接在title标签上创建条件ui片段,但我想知道为什么上面的不起作用,请指教,谢谢.
ORIGINAL JSP(WorkItem.jsp)
<c:forEach var="actionItem" items="${workItem.work_action_list}">
<c:if test="${actionItem.workActionClass.work_action_type_id == '1'}" >
<%@ include file="inc_done_button.jsp" %>
</c:if>
<c:if test="${actionItem.workActionClass.work_action_type_id == '2'}" >
<c:set var="actionItem" value="${actionItem}" scope="request" />
<c:set var="checklist" value="${actionItem.meat}" scope="request" />
<jsp:include page="inc_dynamic_checklist_v.jsp" flush="true" />
</c:if>
etc...
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
原始Java
for (ListIterator<WorkflowInstanceWorkItemAction> actionIter = wfiwi.getWork_action_list().listIterator(); actionIter.hasNext();) {
if ("2".equals(work_action_type_id)) {
ChecklistInstanceForm ciForm = new ChecklistInstanceForm(this, authenticatedUser);
ChecklistInstance ci = null;
ci = (ChecklistInstance) ciForm.getChkLstInstanceByWfiWiaOwner(wfiWorkItemAction, authenticatedUser);
// Get the meat details for this action and inject it into the object
wfiWorkItemAction.setMeat(ci);
} …Run Code Online (Sandbox Code Playgroud) 如果我使用如下,我没有错误,没有输出.为什么p:panelGrid不用ui:repeat?
注意:我不想使用c:forEach因为我已经面临很多JSF问题.
<p:panelGrid>
<ui:repeat value="#{MyBean.dataList}" var="data">
<p:row>
<p:column>
<h:outputText value="#{data.name}"/>
</p:column>
<p:column>
<h:outputText value="#{data.description}"/>
</p:column>
</p:row>
</ui:repeat>
</p:panelGrid>
Run Code Online (Sandbox Code Playgroud)
MyBean.java
public List<Data> getDataList(){
List<Data> result = new ArrayList<Data>();
result.add(new Data("Name 1", "Description 1"));
result.add(new Data("Name 2", "Description 2"));
result.add(new Data("Name 3", "Description 3"));
result.add(new Data("Name 4", "Description 4"));
return result;
}
Run Code Online (Sandbox Code Playgroud)
带有primefaces的预期输出

在一个支持bean中,我定义了一个Map<Integer,String>属性.当试图从xhtml文件中的EL访问地图时,我得不到任何回报.
<h:outputLabel value="#{bean.myMap[0]}">
Run Code Online (Sandbox Code Playgroud)
不返回键0的值.使用String键可以工作.
它适用于a List<String>,但我希望Map有一些稀疏数组(并非所有索引都有值)
我的要求是这样的.在JSF(2.0)页面中,我有一个名为Address的部分.我添加了一个"添加地址"按钮.当用户单击该按钮时,应该动态生成新的地址字段(输入文本)(Line1,Line2,City等..).当我单击提交时,所有值(地址1,地址2 ...地址N)应该转到数组列表.
所以我需要
在线找到合适的JSF文档非常困难,所以如果有人能帮助我,那就太棒了
更新:没有人发布的答案很好,但我想要更强大的东西,比如从Java控制器创建动态UI组件(java bean,使用HtmlPanelGrid组件).我已经能够使用htmlPanelGrid动态创建组件,但我找不到将这些生成的组件绑定到bean中的地址列表的方法(存储所有地址的详细信息)