(问题解决了) 我有一个jsf2-spring-hibernate项目,每当我点击一个按钮时,bean中的构造函数和postconstruct都被调用,我想要执行的实际点击监听器被忽略。注意bean是sessioncoped(而不是请求),可能它应该是viewscoped - 但这会干扰Spring吗?注2:我尝试了ViewScoped和相同的结果。
引起我注意的问题是clickListener(下面)没有执行
<h:selectOneListbox id="listBox" value="#{ScheduleMB.clients}" size="5"
rendered="#{ScheduleMB.showClients}" >
<f:selectItems value="#{ScheduleMB.clientList}" var="c"
itemLabel="#{c.lastName}" itemValue="#{c.lastName}" />
<f:ajax event="click" listener="#{ScheduleMB.clickListener}"
render="group" />
</h:selectOneListbox>
我尝试做的是在用户点击按钮时显示客户列表(上面未显示的按钮,但包含在下面的完整jsf2页面中)。当用户单击列表框时,将激活clicklistener并且bean处理单击的值。
第二次编辑 - 我的facelet包括绑定到bean,这似乎是导致bean在viewscope中重建的原因,而不是sessioncope。
编辑 - 我做了一些更改1)我确保我的bean扩展的类是抽象的。这允许ajax代码工作,但是即使在查看视图时构造函数仍然运行了几次,我不确定应该发生什么。 2)将范围更改为会话,一切正常,其他更改。
所以我的问题是,为什么构造函数在ViewScoped时仍会多次触发?
原始问题:
下面是完整的jsf2页面(以及javabean下面的页面)。
<html 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.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:form>
<h3>TimeOnly</h3>
<t:div style="position: absolute; left: 5px; top: 5px; right: 5px; width: 875px ">
<t:schedule value="#{ScheduleMB.model}" id="schedule1"
binding="#{ScheduleMB.schedule}"
rendered="true" visibleEndHour="18" visibleStartHour="8"
workingEndHour="17" workingStartHour="9" readonly="false"
theme="default" tooltip="true"
submitOnClick="true"
mouseListener="#{ScheduleMB.scheduleClicked}"
action="#{ScheduleMB.scheduleAction}" />
</t:div>
<t:div style="position: absolute; left: 925px; top: 5px; width: 210px; overflow: auto">
<h:panelGrid columns="1">
<t:inputCalendar id="scheduleNavigator"
value="#{ScheduleMB.model.selectedDate}" />
</h:panelGrid>
</t:div>
<t:div style="position: absolute; left: 890px; top: 190px; width: 325px; overflow: auto; font-size: 14px;
text-align: left; height: 270px" >
<h:panelGrid columns="1" rendered="true">
<h:outputLabel>Enter From Date:Time</h:outputLabel>
<t:inputDate popupCalendar="true" type="both" value="#{ScheduleMB.from}"/>
<h:outputLabel>Enter To Date:Time</h:outputLabel>
<t:inputDate popupCalendar="true" type="both" value="#{ScheduleMB.to}"/>
<h:outputLabel for="TitleText" value="Title:"/>
<h:panelGroup id="group">
<h:inputText id="TitleText" value="#{ScheduleMB.title}"/>
<h:commandButton value="Clients" >
<f:ajax event="click" listener="#{ScheduleMB.clientList}"
render="group"/>
</h:commandButton>
<h:selectOneListbox id="listBox" value="#{ScheduleMB.clients}" size="5"
rendered="#{ScheduleMB.showClients}" >
<f:selectItems value="#{ScheduleMB.clientList}" var="c"
itemLabel="#{c.lastName}" itemValue="#{c.lastName}" />
<f:ajax event="click" listener="#{ScheduleMB.clickListener}"
render="group" />
</h:selectOneListbox>
</h:panelGroup>
<h:outputLabel for="DescText" value="Description:"/>
<h:inputText id="DescText" value="#{ScheduleMB.description}"/>
<h:panelGroup>
<h:commandButton
actionListener="#{ScheduleMB.addNewEntry}"
value="Add entry" rendered="#{not ScheduleMB.model.entrySelected}"/>
<h:commandButton
actionListener="#{ScheduleMB.deleteSelectedEntry}"
value="Delete entry"
rendered="#{ScheduleMB.model.entrySelected}"/>
</h:panelGroup>
</h:panelGrid>
</t:div>
</h:form>
</html>
这是Sessionscoped bean(我已经删除了许多不相关的部分):
@ManagedBean(name="ScheduleMB")
@SessionScoped
public class ScheduleMB extends ScheduleBase
implements Serializable
{
Map<String,Object> clientValues = new HashMap<String,Object>();
List<Client> clientValues2 = new ArrayList<Client>();
@ManagedProperty(value="#{UserService}")
IUserService userService;
public ScheduleMB() {
clients="hello"; //I put a breakpoint here just to be sure it was called
}
@PostConstruct
public void init() {
createClientValues();
}
//This part is not executing.
public void clickListener(AjaxBehaviorEvent event){
this.title = clients;
showClients = false;
renderClientList=false;
}
public List<Client> getClientList() {
return clientValues2;
}
private void createClientValues() {
clientValues2 = userService.findAllClients();
}
public void clientList() {
showClients=true;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getClients() {
return clients;
}
public void setClients(String clients) {
this.clients = clients;
}
public boolean isRenderClientList() {
return renderClientList;
}
public void setRenderClientList(boolean renderClientList) {
this.renderClientList = renderClientList;
}
public boolean isShowClients() {
return showClients;
}
public void setShowClients(boolean showClients) {
this.showClients = showClients;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
答案 0 :(得分:0)
找出问题所在以及如何解决问题。最初的症状是我的f:ajax click事件没有触发关联的bean方法,并且尽管是视图编码,但是每次交互都构建了bean。我仔细检查了bean,注意到它的父类不是抽象的,并且范围不一致。我删除了范围并使其抽象化。我还将主bean的范围更改为session,因为facelet绑定到bean,这也会导致在viewscoped下的每次交互中调用构造函数。问题解决了。