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?
Haben Sie UrlEntry in Ihre JSP importiert? –
ja. Ich tat. Ich habe Probleme beim Gießen zu Arraylist –
kurumkan
aber ohne Casting alle funktioniert gut. Kein Fehler – kurumkan