简体   繁体   English

当用户单击时,未调用ajax事件,但jsf2 spring bean调用了构造函数和后构造

[英]ajax event not called but jsf2 spring bean calls constructor and postconstruct when user clicks

(Problem solved) I have a jsf2-spring-hibernate project and everytime I click a button both the constructor and postconstruct in the bean are being called and the actual click listener that I want to execute is being ignored. (问题已解决)我有一个jsf2-spring-hibernate项目,每当我单击一个按钮时,都会调用Bean中的构造函数和postconstruct,而我要执行的实际单击侦听器将被忽略。 Note the bean is sessionscoped (not request), possibly it should be viewscoped - but would this interfere with Spring? 注意,bean是sessionscoped(不是请求)的,可能应该是viewscoped的-但这会干扰Spring吗? Note2: I tried ViewScoped and same result. 注意2:我尝试了ViewScoped和相同的结果。

The problem that clued me in was that the clickListener (below) was not executing 导致我陷入困境的问题是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>

What I am attempting to do is have a list of clients appear when a user clicks a button(button not shown above but included in full jsf2 page below). 我正在尝试做的是在用户单击按钮时显示一个客户端列表(按钮未在上方显示,但包含在下面的完整jsf2页面中)。 When the user clicks the listbox the clicklistener is activated and the bean processes the clicked value. 当用户单击列表框时,将激活clicklistener,并且bean将处理该单击的值。

2nd Edit - My facelet includes binding to the bean, and this seems to be what causes the bean to be reconstructed in viewscope, but not sessionscope. 2nd Edit-我的facelet包含到bean的绑定,这似乎是导致在viewscope中重建bean的原因,而不是sessionscope。

Edit - I made some changes 1) I made sure the class my bean extends was abstract. 编辑-我进行了一些更改1)我确保我的bean扩展的类是抽象的。 That allowed ajax code to work, however constructor still ran several times even when viewscoped, I'm not sure that should be happening. 这样就可以使ajax代码正常工作,但是即使在进行观察时,构造函数仍然运行了几次,我不确定这是否会发生。 2) Changed scope to session and everything works fine with the other changes. 2)将范围更改为会话,并且其他更改一切正常。

So my question is, why is the constructor still firing multiple times when ViewScoped? 所以我的问题是,为什么在ViewScoped时构造函数仍会多次触发?

Original Question: 原始问题:

Below is the full jsf2 page (and below that the javabean). 下面是完整的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>

And here is the Sessionscoped bean (I've eliminated much of the irrelevant parts): 这是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;
}
 }

Figured out what the problem was and how to fix it. 找出问题所在以及如何解决。 The original symptoms were that my f:ajax click event was not firing the associated bean method and that the bean was being constructed with each interaction, despite being viewscoped. 最初的症状是我的f:ajax click事件未触发关联的bean方法,并且尽管进行了观察,但每次交互都在构造该bean。 I Double checked the bean and noticed that its parent class was not abstract and had an inconsistent scope. 我仔细检查了bean,发现它的父类不是抽象的,并且作用域不一致。 I eliminated the scope and made it abstract. 我消除了范围,使其变得抽象。 I also changed the main bean's scope to session because the facelet binds to the bean which will also cause the constructor to be called with each interaction under viewscoped. 我还将主bean的作用域更改为会话,因为facelet绑定到了bean,这也将导致在viewscoped下每次交互时调用构造函数。 Problems solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM