2011-03-15 70 views
0

有一个很小的JSP问题。对于整个比特来说是新的,所以请对我宽大:) 我打算画一张2列的表格:一个用于Facebook好友个人资料图片,另一个用于Facebook好友名称。 我在这个论坛上查了一些参考资料,但他们似乎没有解决。在JSP中显示15个带有名称和个人资料图片的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; 
    } 

} 

有谁看到问题与此代码?

+0

看起来像一个家庭作业 – hawkeye 2011-05-30 10:49:30

+0

是啊 - 这是一个几个星期前:) – 2011-06-08 20:12:16

回答

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/whatever-server-side标签。当这样一个servlet(不推荐)里面,那么你需要进行如下修正:

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

或做这一个JSP中时,那么就写下这些标签平原,而不是与小脚本摆弄。

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