JSF-Ajax调用-这段代码有什么错?
我认为解决此问题的最佳方法就是粘贴我的代码:JSF-Ajax调用-这段代码有什么错?,ajax,jsf,jsf-2,javabeans,Ajax,Jsf,Jsf 2,Javabeans,我认为解决此问题的最佳方法就是粘贴我的代码: 选择器bean @ManagedBean(name="selector") @RequestScoped public class Selector { @ManagedProperty(value="#{param.page}") private String page; private String profilePage; @PostConstruct public void init() {
选择器bean
@ManagedBean(name="selector")
@RequestScoped
public class Selector {
@ManagedProperty(value="#{param.page}")
private String page;
private String profilePage;
@PostConstruct
public void init() {
if(profilePage==null || profilePage.trim().isEmpty()) {
this.profilePage="main";
}
if(page==null || page.trim().isEmpty()) {
this.page="homepage";
}
}
public String getProfilePage() { System.out.println("GET ="+profilePage); return profilePage; }
public void setProfilePage(String profilePage) { this.profilePage=profilePage; }
public String getPage() { return page; }
public void setPage(String page) { this.page=page; }
}
@ManagedBean
@ViewScoped
public class ProfileSelector {
private String profilePage;
@PostConstruct
public void init() {
if(profilePage==null || profilePage.trim().isEmpty()) {
this.profilePage="main";
}
}
public String getProfilePage() { return profilePage; }
public void setProfilePage(String profilePage) { this.profilePage=profilePage; }
}
profile.xhtml
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileEditContent">
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<h:form id="formProfileEdit" prependId="false">
<h:panelGroup layout="block">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{selector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileEditContent"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{selector.profilePage}" value="main" />
<f:ajax event="action" render=":profileEdit"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY IM HERE" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContent">
<h:form id="formProfile" prependId="false">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:panelGroup rendered="#{profileSelector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup layout="block">
<h:outputLabel value="MAIN" />
<h:panelGroup layout="block">
<h:commandButton value="Edit Button">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="edit" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContentEdit">
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<h:panelGroup layout="block">
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block" styleClass="profilo_3">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileContentEdit"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="main" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY Im HERE" />
</h:panelGroup>
</h:panelGroup>
profile\u edit.xhtml
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileEditContent">
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<h:form id="formProfileEdit" prependId="false">
<h:panelGroup layout="block">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{selector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileEditContent"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{selector.profilePage}" value="main" />
<f:ajax event="action" render=":profileEdit"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY IM HERE" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContent">
<h:form id="formProfile" prependId="false">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:panelGroup rendered="#{profileSelector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup layout="block">
<h:outputLabel value="MAIN" />
<h:panelGroup layout="block">
<h:commandButton value="Edit Button">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="edit" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContentEdit">
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<h:panelGroup layout="block">
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block" styleClass="profilo_3">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileContentEdit"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="main" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY Im HERE" />
</h:panelGroup>
</h:panelGroup>
profile.xhtml
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileEditContent">
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<h:form id="formProfileEdit" prependId="false">
<h:panelGroup layout="block">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{selector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileEditContent"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{selector.profilePage}" value="main" />
<f:ajax event="action" render=":profileEdit"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY IM HERE" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContent">
<h:form id="formProfile" prependId="false">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:panelGroup rendered="#{profileSelector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup layout="block">
<h:outputLabel value="MAIN" />
<h:panelGroup layout="block">
<h:commandButton value="Edit Button">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="edit" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContentEdit">
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<h:panelGroup layout="block">
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block" styleClass="profilo_3">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileContentEdit"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="main" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY Im HERE" />
</h:panelGroup>
</h:panelGroup>
profile\u edit.xhtml
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileEditContent">
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<h:form id="formProfileEdit" prependId="false">
<h:panelGroup layout="block">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{selector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileEditContent"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{selector.profilePage}" value="main" />
<f:ajax event="action" render=":profileEdit"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY IM HERE" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContent">
<h:form id="formProfile" prependId="false">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:panelGroup rendered="#{profileSelector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" />
</h:panelGroup>
</h:form>
</h:panelGroup>
<h:panelGroup layout="block">
<h:outputLabel value="MAIN" />
<h:panelGroup layout="block">
<h:commandButton value="Edit Button">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="edit" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup layout="block" id="profileContentEdit">
<h:panelGroup rendered="#{profileSelector.profilePage=='edit'}">
<h:panelGroup layout="block">
<h:outputLabel value="EDIT" />
<h:panelGroup layout="block" styleClass="profilo_3">
<h:commandButton value="Confirm Edit">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="editConfirm" />
<f:ajax event="action" render=":profileContentEdit"/>
</h:commandButton>
<h:commandButton value="Back">
<f:setPropertyActionListener target="#{profileSelector.profilePage}" value="main" />
<f:ajax event="action" render=":profileContent"/>
</h:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup rendered="#{profileSelector.profilePage=='editConfirm'}">
<h:outputLabel value="FINALLY Im HERE" />
</h:panelGroup>
</h:panelGroup>
嗯,这就复杂了。是否调用
ui命令
操作还取决于组件或其父级之一的呈现
属性的结果。由于bean在请求范围内,profilePage
在下一个请求中默认返回到main
,因此编辑部分的rendered
属性求值为false
,因此编辑部分中的按钮不会调用任何操作。这一点在你的报告中得到了回答
理论上,标记bean@ViewScoped
应该可以解决这个问题,因为它在后续视图中保留bean状态。然而,在您的特殊情况下,有两个问题妨碍它正常工作
首先,您使用的是一个@ManagedProperty
,它引用了一个范围较短的值({param}
基本上是请求范围)。您需要将profilePage
拆分为另一个bean,并将其标记为@ViewScoped
其次,由于JSF2()中当前仍有一个未解决的bug,您的特定案例仍然无法工作,因为您有多个
处于不同的呈现
状态,它们都连接到同一个bean。这种特殊情况将导致返回的响应中完全缺少javax.faces.ViewState
。这将导致视图范围的bean被丢弃并重新创建(并且profilePage
再次默认为main
)。作为临时解决方法,您需要在profile.xhtml
中将表单提取并合并为一个表单,作为第一个
的直接子级
更新:如果您唯一关心的是要将bean相互连接,则可以按如下方式拆分bean:
@ManagedBean
@RequestScoped
public class Selector {
@ManagedProperty(value="#{param.page}")
private String page;
@ManagedProperty(value="#{profileSelector}")
private ProfileSelector profileSelector;
// ...
}
@ManagedBean
@ViewScoped
public class Selector {
private String page;
private String profilePage;
@PostConstruct
public void init() {
page = FacesContext.getCurrentInstance().getRequestParameterMap().get("page");
}
// ...
}
然后,视图范围的bean可以通过这种方式在请求范围的bean中访问 或者,如果您真的想要一个bean,您也可以作为一种解决方法,替换
@ManagedProperty
,如下所示:
@ManagedBean
@RequestScoped
public class Selector {
@ManagedProperty(value="#{param.page}")
private String page;
@ManagedProperty(value="#{profileSelector}")
private ProfileSelector profileSelector;
// ...
}
@ManagedBean
@ViewScoped
public class Selector {
private String page;
private String profilePage;
@PostConstruct
public void init() {
page = FacesContext.getCurrentInstance().getRequestParameterMap().get("page");
}
// ...
}
哦。至少现在我明白了为什么它会在游行开始时创建带有“main”的组件树。我想我需要改变策略,因为我需要带有RequestScope:)的选择器bean。不幸的是,我没有看到任何其他方法可以用jsf实现带有ajax调用的dinamic菜单……你真的想要
@ViewScoped
。请参阅答案更新如何处理此问题。是的,我分成两个不同的bean(最佳可见性)。已尝试,但目前无法加载渲染的。我再试试!现在,我有两个豆子。我也只需要放一个全局表单?事实上,我不明白为什么它不能在多个渲染中处理多个表单:)是的,你需要有一个表单,它被放置在带有渲染的条件的部分之外,并且绑定到同一个bean。按照答案中的建议,将其放在profile.xhtml
中。