当我在我的java Bean中使用FacebookClient类时,xhtml不会运行

时间:2013-04-22 16:02:40

标签: jsf java-ee

我尝试了这些简单的java和xhtml代码,但它不起作用。

@ManagedBean(name = "connecter")
@RequestScoped
@SessionScoped
public class Connecter 
{


    int nombre;

    public Connecter()
    {

//这里我将输入我从Graph API Explorer //

获得的访问令牌
        FacebookClient facebookClient = new DefaultFacebookClient("");
        Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class, Parameter.with("fields", "id, name,picture"));

        nombre = myFriends.getData().size();

    }

    public int getNombre()
    {

        return nombre;
    }

    public void setNombre(int nombre)
    {
        this.nombre = nombre;
    }

}

//这是我的web.xml文件,包括servlet映射。那么如何检查URL是否落在Faces Servlet URL中?

     <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>..</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
   <context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
  <context-param>
    <description>
    ..</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
    ..'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
    ..
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>
</web-app>

1 个答案:

答案 0 :(得分:1)

托管bean不能同时拥有多个范围。从托管bean中删除@RequestScoped@SessionScoped注释。

您的托管bean逻辑不应该在您的bean构造函数中,而是使用@PostConstruct方法:

public class Connecter {
    int nombre;
    public Connecter() {
    }
    @PostConstruct
    public void init() {
        //be sure to have this myFriend object declared and initialized here as well
        //also, manage the Exceptions here...
        //this is a plain basic example
        try {
            FacebookClient facebookClient = new DefaultFacebookClient("");
            Connection<User> myFriends = facebookClient.fetchConnection(
                "me/friends", User.class,
                 Parameter.with("fields", "id, name,picture"));
            //here I will ...
            nombre = myFriends.getData().size();
         } catch (Exception e) {
            e.printStackTrace();
            nombre = 0;
         }
    }
    //getters, setters, other methods...
}