0

我尝试了这些简单的 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>
4

1 回答 1

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...
}
于 2013-04-22T16:05:19.673 回答