假设我上课了:
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(){}
}
这是来自控制器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");
这是index.jsp的代码的一部分 这很好用
<%=request.getSession().getAttribute("urls")%>
但这不起作用:
<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%>
有错误
UrlEntry cannot be resolved to a type
<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%>
我做错了什么? 我应该将UrlEntry声明为可序列化吗? 也许构造函数有些问题?
答案 0 :(得分:4)
您必须先在jsp页面中导入UrlEntry类。
<%@page import="packageName.UrlEntry"%>
答案 1 :(得分:3)
JSP编译器引发错误。它告诉您需要将UrlEntry
的import指令添加到JSP页面。