MVC模式中视图层输出数据库信息为null
在做一个类似微博的网站,要求输出粉丝和关注的人。但在实际网站视图层上,只能输出关注的人,要输出粉丝时只能输出null。请求大神帮找下原因。
DAO实现类
public List<Follower> showFollowing(String userId)throws Exception //显示关注的人
{
List<Follower> following=new ArrayList<Follower>();
String sql="SELECT following_id from followers WHERE user_id=?";//user_id 为用户自己,following_id 为关注的人
try{
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1, userId);
ResultSet rs=this.pstmt.executeQuery();
while(rs.next()){
Follower follower=new Follower();
follower.setFollowingid(rs.getString("following_id"));
following.add(follower);
}
}catch(Exception e){
}
this.pstmt.close();
return following;
}
public List<Follower> showFollowers(String followingId)throws Exception{
List<Follower> followers=new ArrayList<Follower>();
String sql="SELECT user_id FROM followers WHERE following_id=?"; // following_id为用户自己,user_id 为粉丝
try{
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1, followingId);
ResultSet rs=this.pstmt.executeQuery();
while(rs.next()){
Follower follower=new Follower();
follower.setUserid(rs.getString("user_id"));
followers.add(follower);
}
}catch(Exception e){
}
this.pstmt.close();
return followers;
}
Servlet类
if("showfollowing".equals(method))//显示正在关注的人
{
try
{
String userid=new String(request.getParameter("userid").getBytes("ISO-8859-1"),"utf-8");
request.setAttribute("all", DAOFactory.getIFollowDAOInstance().showFollowing(userid));
request.getRequestDispatcher(path3).forward(request,response);
}catch(Exception e)
{
}
}
if("showfollowers".equals(method))//显示粉丝
{
try
{
String followingid=new String(request.getParameter("followingid").getBytes("ISO-8859-1"),"utf-8");
request.setAttribute("all", DAOFactory.getIFollowDAOInstance().showFollowers(followingid));
request.getRequestDispatcher(path2).forward(request,response);
}catch(Exception e)
{
}
}
对象vo类
package weibodemo.vo;
public class Follower {
private String userid;
private String followingid;
public String getUserid(){
return userid;
}
public void setUserid(String userid){
this.userid=userid;
}
public String getFollowingid(){
return followingid;
}
public void setFollowingid(String followingid){
this.followingid=followingid;
}
}
视图层的部分代码,负责输出粉丝和关注者
<%
if(session.getAttribute("username")!=null)
{
%>
<%
int i=0;
List all=null;
all=(List)request.getAttribute("all");
%>
<%
Iterator iter=all.iterator();
while(iter.hasNext()){
Follower follower=(Follower)iter.next();
i++;
//从数据库取出内容
String username=follower.getUserid();
%>
<%
if(session.getAttribute("username")!=null)
{
%>
<%
int i=0;
List all=null;
all=(List)request.getAttribute("all");
%>
<%
Iterator iter=all.iterator();
while(iter.hasNext()){
Follower follower=(Follower)iter.next();
i++;
//从数据库取出内容
String username=follower.getFollowingid();
%>
淡淡的蛋疼
10 years, 4 months ago