(问题已解决)我有一个 jsf2-spring-hibernate 项目,每次单击按钮时,bean 中的构造函数和 postconstruct 都会被调用,而我要执行的实际单击侦听器将被忽略。请注意 bean 是 sessionscoped (不是请求),可能它应该是 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 中重建的原因,但不是 sessionscope。
编辑 - 我做了一些更改 1) 我确保我的 bean 扩展的类是抽象的。这允许 ajax 代码工作,但是即使在 viewscoped 下,构造函数仍然运行了几次,我不确定这是否应该发生。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;
}
}