2011-07-14 15 views
4

私はJava EE 6の初心者で、最近会話をしていました。私は、JSFページがロードされるとすぐに(Seamのように)会話を開始する方法を見つけることができませんでした。これは実行可能ですか?ページの読み込みを開始する

答えて

4

これは可能です。

ページ:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <f:view contentType="text/html"> 
     <f:metadata> 
      <f:event type="preRenderView" listener="#{myBean.preRenderView}"/> 
     </f:metadata> 
     <h:head> 
      <title>My Page</title> 
     </h:head> 
     <h:body> 
      <!-- Body here --> 
     </h:body> 
    </f:view> 
</html> 

豆:

import java.io.Serializable; 
import javax.enterprise.context.Conversation; 
import javax.enterprise.context.ConversationScoped; 
import javax.faces.event.ComponentSystemEvent; 
import javax.inject.Inject; 
import javax.inject.Named; 

@Named 
@ConversationScoped 
public class MyBean implements Serializable { 
    public void preRenderView(ComponentSystemEvent e) { 
     String currentViewId = FacesContext.getCurrentInstance().getViewRoot().getViewId(); 
     if (CONVERSATION_START_PAGE.equals(currentViewId)) { 
      conversation.begin(); 
    } 

    @Inject 
    private Conversation conversation; 

    private static final String CONVERSATION_START_PAGE = "/foo/bar/start-page.xhtml"; 
} 
関連する問題