我正在尝试编写一个简单的JSF页面,该页面最初包含两个子视图/包含,并将支持bean传递给其中一个包含,因为包含是可重用的(并且在较大的应用程序中可能包含多次)
I am trying to write a simple JSF page that initially includes two subviews/includes, and pass the backing bean to one of the includes because the include is meant to be reusable (and possibly included more than once in a larger application).
这是相关代码:
page1.xhtml
page1.xhtml
<ui:composition xmlns="www.w3/1999/xhtml" xmlns:h="xmlns.jcp/jsf/html" xmlns:p="xmlns.jcp/jsf/passthrough" xmlns:ui="xmlns.jcp/jsf/facelets" xmlns:f="xmlns.jcp/jsf/core" xmlns:pf="primefaces/ui" xmlns:c="java.sun/jsp/jstl/core" template="templates/template1.xhtml"> <ui:define name="subheader"> Person Details </ui:define> <ui:define name="bodyContent"> <h:form> <c:forEach items="#{page1Bean.blockNames}" var="blockName"> <c:choose> <c:when test="#{blockName} eq 'block1'"> <ui:include src="block1.xhtml" > <ui:param name="bean" value="#{page1Bean.myBean}"/> </ui:include> </c:when> <c:otherwise> <ui:include src="#{blockName}.xhtml" /> </c:otherwise> </c:choose> </c:forEach> <pf:commandButton value="Ajax Button" action="#{page1Bean.ajaxSubmit()}"/> </h:form> </ui:define> </ui:composition>block1.xhtml
block1.xhtml
<ui:composition xmlns="www.w3/1999/xhtml" xmlns:f="java.sun/jsf/core" xmlns:h="java.sun/jsf/html" xmlns:ui="java.sun/jsf/facelets" xmlns:p="xmlns.jcp/jsf/passthrough" xmlns:pf="primefaces/ui"> <h3>This is the first block</h3> <h:panelGrid columns="3" style="margin-bottom:10px" cellpadding="5"> <pf:outputLabel for="firstName" value="Nombre:" /> <pf:inputText id="firstName" value="#{bean.firstName}" /> <h:message id="firstNameMessage" for="firstName" /> </h:panelGrid> </ui:composition>Page1Bean.java
Page1Bean.java
package com.views.prototype; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; @ManagedBean public class Page1Bean { private List<String> blockNames; private Block1Bean myBean; public Page1Bean() { blockNames = new ArrayList<String>(); blockNames.add("block2"); blockNames.add("block1"); myBean = new Block1Bean(); } public void ajaxSubmit() { } @SuppressWarnings("unchecked") public static <T> T findBean(String beanName) { FacesContext context = FacesContext.getCurrentInstance(); return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class); } public List<String> getBlockNames() { return blockNames; } public void setBlockNames(List<String> blockNames) { this.blockNames = blockNames; } public Block1Bean getMyBean() { return myBean; } public void setMyBean(Block1Bean myBean) { this.myBean = myBean; } }Block1Bean.java
Block1Bean.java
package com.views.prototype; import javax.faces.bean.ManagedBean; @ManagedBean public class Block1Bean { private String firstName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Override public String toString() { return "Block1Bean [firstName=" + firstName + "]"; } }页面显示正常,但是当我单击提交"时,我得到:
The page renders ok but when I click submit I get:
value =#{bean.firstName}":无法到达目标,标识符"bean"解析为空
value="#{bean.firstName}": Target Unreachable, identifier 'bean' resolved to null
据我所知,我使用的语法与这些示例的解决方案相同:
As far as I can see I am using the same syntax as the solutions to these examples:
多次重复使用同一页面
将支持bean作为参数传递到Facelet包含
关于我要去哪里的任何想法吗?
Any ideas on where I am going wrong?
推荐答案问题似乎是JSTL和JSF之间的冲突,如以下几个线程所述:
The problem turns out to be the clash between JSTL and JSF, as noted on several threads:
JSTL c:选择c:when在JSF页面中无法使用
指定< ui内的元素的条件渲染:repeat> ;? < c:if>似乎不起作用
因此,我重写了不使用JSTL的循环,它摆脱了原始问题中的紧迫问题:
So I've rewritten the loop not to use JSTL and it gets rid of the immediate problem in the original question:
<ui:repeat var="blockName" value="#{page1Bean.blockNames}" varStatus="status"> <ui:fragment rendered="#{blockName == 'block1'}"> <ui:include src="#{page1Bean.theBlockName}" > <ui:param name="bean" value="#{page1Bean.myBean}"/> </ui:include> </ui:fragment> <ui:fragment rendered="#{blockName != 'block1' }"> <ui:include src="block2.xhtml" /> </ui:fragment> </ui:repeat>(这在功能上不等同于已损坏的目的,但要点是它不使用JSTL).
(This isn't functionally equivalent to the intended purpose of the broken one however the point is it doesn't use JSTL).
更多推荐
JSF通过Bean来查看失败
发布评论