有一个小小的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>");
在UserWithPicture.java类中实现getPicture()方法:
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;
}
}
有没有人看到此代码的问题?
答案 0 :(得分:0)
下面,
p.println("<table>" +
"<c:forEach>" +
"<tr>" +
"<td>" + friends.getPicture()
"</td>" +
"<td>" + friends.name
"</td>" +
"</tr>" +
"</c:forEach>" +
"</table>");
你犯了一个概念上的错误。您正在向浏览器发送<c:forEach>
标记。浏览器只能理解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>
答案 1 :(得分:0)
这就是你正在寻找的东西。
http://www.bitspedia.com/2012/01/how-to-get-facebook-friends-list-in.html
本文介绍了如何使用java API(RestFB)获取Facebook好友。还附上了一个示例项目,展示了它。