在 JSP 中显示 15 位 Facebook 好友的列表以及姓名和个人资料图片

发布于 2024-10-22 01:10:42 字数 3107 浏览 1 评论 0原文

遇到一个 JSP 小问题。我对这一切都很陌生,所以请宽容我:) 我打算画一张有两列的表格:一列用于 Facebook 好友的个人资料图片,另一列用于 Facebook 好友的姓名。 我在这个论坛上检查了一些参考资料,但似乎没有成功。

基本代码结构如下:

/*
 * @(#)Friends.java 1.0  
 *
 */


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.Parameter;
import com.restfb.exception.FacebookException;



/**
 * Facebook Application Friends Servlet
 * 
 * @version 3.0
 */
public class Friends extends HttpServlet {

   private static final long serialVersionUID = 1L;

   private static final int MAX_FRIENDS = 15;

   @Override
   public void doGet(final HttpServletRequest request, final HttpServletResponse response)
         throws ServletException, IOException {

      // set MIME type and encoding
      response.setContentType("text/html");
      response.setCharacterEncoding("UTF-8");

      // get writer for output
      final PrintWriter p = response.getWriter();

      // make sure that we have obtained an access token, otherwise redirect
      // to login
      final String accessToken = request.getParameter("access_token");
      if (accessToken == null) {
         response.sendRedirect(Config.getValue("LOGIN_URL"));
         return;
      }

      // get client
      final DefaultFacebookClient client = new DefaultFacebookClient(accessToken);

      // retrieve the document with all friend user ids
      try {
         final Connection<UserWithPicture> friends = client.fetchConnection("me/friends",
               UserWithPicture.class, Parameter.with("fields", "name, picture"),
               Parameter.with("limit", Friends.MAX_FRIENDS));

         p.println( /** Here goes the code for table display **/);
      } catch (final FacebookException e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }

      p.flush();
      p.close();
   }

   @Override
   public void doPost(final HttpServletRequest request, final HttpServletResponse response)
         throws ServletException, IOException {

      this.doGet(request, response);
   }

}

现在,我已经编写了以下代码,但由于任何奇怪的原因,它不起作用。这里是:

 p.println("<table>" +
            "<c:forEach>" +
                "<tr>" +
                    "<td>" + friends.getPicture()
                    "</td>" +
                    "<td>" + friends.name
                    "</td>" +
                "</tr>" +
            "</c:forEach>" +
          "</table>");

getPicture() 方法在 UserWithPicture.java 类中实现的位置:

import com.restfb.Facebook;
import com.restfb.types.User;

/**
 * Model class for a User with picture
 * @version 1.0
 */
public class UserWithPicture extends User {

   @Facebook
   private String picture;

   public String getPicture() {

      return this.picture;
   }

}

有人看到这段代码有问题吗?

Got a tiny JSP problem. Am new to the whole bit, so please be lenient with me :)
I intend to draw a table with 2 columns: One for facebook friends profiles pictures and the other for facebook friends names.
I've checked a few references on this forum, but they didn't seem to work out.

Here goes the basic code structure:

/*
 * @(#)Friends.java 1.0  
 *
 */


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.Parameter;
import com.restfb.exception.FacebookException;



/**
 * Facebook Application Friends Servlet
 * 
 * @version 3.0
 */
public class Friends extends HttpServlet {

   private static final long serialVersionUID = 1L;

   private static final int MAX_FRIENDS = 15;

   @Override
   public void doGet(final HttpServletRequest request, final HttpServletResponse response)
         throws ServletException, IOException {

      // set MIME type and encoding
      response.setContentType("text/html");
      response.setCharacterEncoding("UTF-8");

      // get writer for output
      final PrintWriter p = response.getWriter();

      // make sure that we have obtained an access token, otherwise redirect
      // to login
      final String accessToken = request.getParameter("access_token");
      if (accessToken == null) {
         response.sendRedirect(Config.getValue("LOGIN_URL"));
         return;
      }

      // get client
      final DefaultFacebookClient client = new DefaultFacebookClient(accessToken);

      // retrieve the document with all friend user ids
      try {
         final Connection<UserWithPicture> friends = client.fetchConnection("me/friends",
               UserWithPicture.class, Parameter.with("fields", "name, picture"),
               Parameter.with("limit", Friends.MAX_FRIENDS));

         p.println( /** Here goes the code for table display **/);
      } catch (final FacebookException e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }

      p.flush();
      p.close();
   }

   @Override
   public void doPost(final HttpServletRequest request, final HttpServletResponse response)
         throws ServletException, IOException {

      this.doGet(request, response);
   }

}

Now, I've written the following code and it's not working for whatever freaky reason there might be. Here it goes:

 p.println("<table>" +
            "<c:forEach>" +
                "<tr>" +
                    "<td>" + friends.getPicture()
                    "</td>" +
                    "<td>" + friends.name
                    "</td>" +
                "</tr>" +
            "</c:forEach>" +
          "</table>");

Where the getPicture() method is implemented in the UserWithPicture.java Class:

import com.restfb.Facebook;
import com.restfb.types.User;

/**
 * Model class for a User with picture
 * @version 1.0
 */
public class UserWithPicture extends User {

   @Facebook
   private String picture;

   public String getPicture() {

      return this.picture;
   }

}

Does anyone see the problem with this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-10-29 01:10:42

在这里,

 p.println("<table>" +
            "<c:forEach>" +
                "<tr>" +
                    "<td>" + friends.getPicture()
                    "</td>" +
                    "<td>" + friends.name
                    "</td>" +
                "</tr>" +
            "</c:forEach>" +
          "</table>");

您犯了一个概念错误。您正在向浏览器发送 标记。浏览器只能识别 HTML。浏览器不理解 Java/JSP/JSTL/任何服务器端标记。当在 servlet 中执行此操作(不推荐)时,您需要按如下方式修复它:

p.println("<table>");
for (Friend friend : friends) {
    p.println("<tr><td>" + friend.getPicture() + "</td><td>" + friend.getName() + "</td></tr>");
}
p.println("</table>");

或者当在 JSP 中执行此操作时,只需简单地写下这些标记,而不是摆弄 scriptlet

<table>
    <c:forEach items="${friends}" var="friend">
        <tr><td>${friend.picture}</td><td>${friend.name}</td></tr>
    </c:forEach>
</table>

Here,

 p.println("<table>" +
            "<c:forEach>" +
                "<tr>" +
                    "<td>" + friends.getPicture()
                    "</td>" +
                    "<td>" + friends.name
                    "</td>" +
                "</tr>" +
            "</c:forEach>" +
          "</table>");

you're making a conceptual mistake. You're sending <c:forEach> tag plain to the browser. The browser only understands HTML. The browser doesn't understand Java/JSP/JSTL/whatever-server-side tags. When doing this inside a servlet (not recommended), then you need to fix it as follows:

p.println("<table>");
for (Friend friend : friends) {
    p.println("<tr><td>" + friend.getPicture() + "</td><td>" + friend.getName() + "</td></tr>");
}
p.println("</table>");

Or when doing this inside a JSP, then just write down those tags plain instead of fiddling with scriptlets.

<table>
    <c:forEach items="${friends}" var="friend">
        <tr><td>${friend.picture}</td><td>${friend.name}</td></tr>
    </c:forEach>
</table>
看透却不说透 2024-10-29 01:10:42

这正是您正在寻找的东西。

http://www.bitspedia.com /2012/01/how-to-get-facebook-friends-list-in.html

本文介绍了如何使用 java API (RestFB) 获取 Facebook 好友。还附上了一个示例项目来演示它。

Here is axactly what you are looking for.

http://www.bitspedia.com/2012/01/how-to-get-facebook-friends-list-in.html

The article explains how you can fetch Facebook Friends using java API (RestFB). A sample project is also attached which demonstrate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文