未调用ajax事件,但JSF2Springbean在用户单击时调用构造函数和后构造函数
(问题已解决) 我有一个jsf2 spring hibernate项目,每次单击一个按钮,bean中的构造函数和后构造函数都会被调用,而我想要执行的实际单击侦听器会被忽略。注意bean是sessionscoped(不是request),可能应该是viewscope,但是这会干扰Spring吗?注2:我尝试了ViewScope,结果相同 问题是clickListener(下面)没有执行未调用ajax事件,但JSF2Springbean在用户单击时调用构造函数和后构造函数,ajax,jsf,jsf-2,Ajax,Jsf,Jsf 2,(问题已解决) 我有一个jsf2 spring hibernate项目,每次单击一个按钮,bean中的构造函数和后构造函数都会被调用,而我想要执行的实际单击侦听器会被忽略。注意bean是sessionscoped(不是request),可能应该是viewscope,但是这会干扰Spring吗?注2:我尝试了ViewScope,结果相同 问题是clickListener(下面)没有执行 <h:selectOneListbox id="listBox" value="#{ScheduleMB.
<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,这似乎是导致在viewscope中重建bean的原因,而不是sessionscope
编辑-我做了一些更改1)我确保我的bean扩展的类是抽象的。这使得ajax代码能够工作,但即使在ViewScope中,构造函数仍会运行多次,我不确定这是否应该发生。2) 将作用域更改为会话,并且在其他更改中一切正常
所以我的问题是,为什么在ViewScope中构造函数仍会多次触发
原始问题:
下面是完整的jsf2页面(下面是javabean)
仅限时间
输入起始日期:时间
输入截止日期:时间
下面是Sessionscoped bean(我已经删除了很多不相关的部分):
@ManagedBean(name=“ScheduleMB”)
@会议范围
公共类ScheduleMB扩展ScheduleBase
实现序列化接口
{
Map clientValues=newhashmap();
List clientValues2=新的ArrayList();
@ManagedProperty(value=“#{UserService}”)
IUserService用户服务;
公共调度MB(){
clients=“hello”//我在这里放置了一个断点,以确保它被调用
}
@施工后
公共void init(){
createClientValues();
}
//此部分未执行。
公共void clickListener(AjaxBehaviorEvent事件){
this.title=客户;
showClients=false;
renderClientList=false;
}
公共列表getClientList(){
返回clientValues2;
}
私有void createClientValues(){
clientValues2=userService.findAllClients();
}
public void clientList(){
showClients=true;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getClients(){
返回客户;
}
公共void setClients(字符串客户端){
这个。客户=客户;
}
公共布尔值isRenderClientList(){
返回renderClientList;
}
公共void setRenderClientList(布尔renderClientList){
this.renderClientList=renderClientList;
}
公共布尔值IsShowClient(){
回报客户;
}
公共void SetShowClient(布尔ShowClient){
this.showClients=showClients;
}
公共IUserService getUserService(){
返回用户服务;
}
公共void setUserService(IUserService userService){
this.userService=userService;
}
}
找出了问题所在以及解决方法。最初的症状是,我的f:ajax点击事件没有触发相关联的bean方法,并且bean是通过每次交互构造的,尽管它是viewscope。我仔细检查了bean,发现它的父类不是抽象的,并且有一个不一致的作用域。我去掉了范围,把它抽象了。我还将主bean的作用域更改为session,因为facelet绑定到bean,这也将导致在viewscoped下的每个交互中调用构造函数。问题解决。您使用的是什么版本的JSF?查看并更新是否适用于您
<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>
@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;
}
}