开发者

JSF 2.0 Ajax listener in SelectOneMenu tag is not working

PROBLEM SOLVED!! There was a validationerror on my backing bean telling me something like this: Conversion Error setting value 'Africa (AFR)' for 'null Converter'.

Because of the JSF lifesycle the listener method on my backingbean wasn't called


I'm havinga problem with JSF2. I have a selectOneMenu tag with an ajax tag that should fire a listener when its value is changed. The xhtml page looks like the following (stripped some things out)

<h:selectOneMenu id="selectedContinent" value="#{searchExtendedAction.destination.continent}">
            <f:selectItems value="#{searchExtendedAction.continents}" />
            <f:ajax render="locationSelector" listener="#{searchExtendedAction.doUpdateLocation开发者_如何学Gos}" />
</h:selectOneMenu>

In my backing bean I have the following (Left some properties out):

@ManagedBean
@SessionScoped
public class SearchExtendedAction implements Serializable{


// Left some properties out


    @Valid
    private Location departure;

    @PostConstruct
    public void init(){
        System.err.println("INIT called");
        allContinents = continentService.getAllContinents();
    }

    @EJB
    private ContinentService continentService;

    public void doExtendedSearch()
    {
        System.err.println("SEARCH");
    }
    public void doUpdateLocations(AjaxBehaviorEvent event)
    {
        System.err.println("BONZAI");
    }

    public List<SelectItem> getContinents()
    {
        List<SelectItem> continents= new ArrayList<SelectItem>();
        continents.add(new SelectItem("--- Select Continent ---"));

        for(Continent c : continentService.getAllContinents()){
            continents.add(new SelectItem(c.getName()));
        }

        return continents;
    }
}

Now when I pick a value from my selectOneMenu a HTTP Post is fired. And I know this because my PostConstruct method is called and the message pops up in my console. But the listener is not called. And I have no idea why.

Some more information about the technologies I'm using:

16:21:08,282 INFO  [AbstractServer] Starting: JBossAS [6.1.0.Final "Neo"]
16:21:11,725 INFO  [ServerInfo] Java version: 1.7.0,Oracle Corporation
16:21:11,725 INFO  [ServerInfo] Java Runtime: Java(TM) SE Runtime Environment (build 1.7.0-b147)
16:21:11,725 INFO  [ServerInfo] Java VM: Java HotSpot(TM) 64-Bit Server VM 21.0-b17,Oracle Corporation
16:21:11,726 INFO  [ServerInfo] OS-System: Windows 7 6.1,amd64
16:21:19,067 INFO  [AbstractServerConfig] JBoss Web Services - Stack CXF Server 3.4.1.GA
16:21:19,981 INFO  [JSFImplManagementDeployer] Initialized 3 JSF configurations: [Mojarra-1.2, MyFaces-2.0, Mojarra-2.0]


Now when I pick a value from my selectOneMenu a HTTP Post is fired. And I know this because my PostConstruct method is called and the message pops up in my console.

It is not normal that the @PostConstruct of a session scoped bean is fired at that moment. It should be fired only once during bean's construction whenever you request the view for the first time in the session. Are you sure that the @SessionScoped is of type javax.faces.bean.SessionScoped and thus not javax.enterprise.context.SessionScoped?

Update: as per the comments, that issue has been fixed. However, it didn't solve the concrete problem of the listener not being invoked. It turns out that a validation error has occurred. That can happen when the equals() method of the selected item doesn't return true for any of the items in the list during the apply request values phase. You could get the exact validation error by supplying a

<h:messages id="messages" />

and include its ID in the render attribute

<f:ajax render="locationSelector messages" ... /> 

Unrelated to the concrete problem, the @ViewScoped is a better choice for this. This way every browser window/tab within the same browser session has its own instance of the bean. A session scoped the bean is namely shared between all browser windows/tabs within the same session which may lead to "wtf?" behaviour which is thus bad for User Experience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜