2011-03-15 20 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> 
相關問題