2016-05-25 8 views
1

Angenommen, ich habe Klasse:retreiving Arraylist als Session-Attribut

import java.io.Serializable; 


public class UrlEntry implements Serializable 
{ 
    private String shortUrl; 
    private String longUrl; 
    private long clicks; 


    public UrlEntry(String shortUrl, String longUrl, long clicks) 
    { 
     this.shortUrl = shortUrl; 
     this.longUrl = longUrl; 
     this.clicks = clicks; 
    } 


    public String getShortUrl() 
    { 
     return shortUrl; 
    } 
    public void setShortUrl(String shortUrl) 
    { 
     this.shortUrl = shortUrl; 
    } 
    public String getLongUrl() 
    { 
     return longUrl; 
    } 
    public void setLongUrl(String longUrl) 
    { 
     this.longUrl = longUrl; 
    } 
    public long getClicks() 
    { 
     return clicks; 
    } 
    public void setClicks(long clicks) 
    { 
     this.clicks = clicks; 
    } 

    public UrlEntry(){} 


} 

Dies ist Code von Controller-Servlet

HttpSession session = request.getSession(true); 
ArrayList<UrlEntry> list = new ArrayList<UrlEntry>(); 
list.add(new UrlEntry("abc","site1.com",1)); 
list.add(new UrlEntry("def","site2.com",2)); 
list.add(new UrlEntry("ghi","site3.com",3)); 
session.setAttribute("urls", list);      
response.sendRedirect("index.jsp"); 

Dies ist Teil des Codes von index.jsp das funktioniert gut

<%=request.getSession().getAttribute("urls")%> 

aber das funktioniert nicht:

<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%> 

mit Fehler

UrlEntry cannot be resolved to a type 
<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%> 

Was mache ich falsch? Soll ich UrlEntry als serialisierbar deklarieren? Vielleicht einige Probleme mit dem Konstruktor?

+1

Haben Sie UrlEntry in Ihre JSP importiert? –

+0

ja. Ich tat. Ich habe Probleme beim Gießen zu Arraylist kurumkan

+0

aber ohne Casting alle funktioniert gut. Kein Fehler – kurumkan

Antwort

4

Sie müssen zuerst die UrlEntry-Klasse auf der JSP-Seite importieren.

<%@page import="packageName.UrlEntry"%> 
3

Der Fehler wird vom JSP-Compiler ausgelöst. Es besagt, dass Sie eine Import-Direktive für UrlEntry der JSP-Seite hinzufügen müssen.

Verwandte Themen