求助:struts框架 分层出现
空指针问题
问题是: 页面访问时候 如果 用注释的代码 进入 接口 在执行实现代码会出现空指针 可是把实现代码写在userManager里(就是注释下面的代码) 就能正常现实用户 怎么回事呢? 请大家帮帮忙
下面是userlistAction
package com.bjsxt.struts.user;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class userListAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List userlist = userManager.getInstance().findUser();
request.setAttribute("userlist", userlist);
return mapping.findForward("success");
}
}
下面是userManager
package com.bjsxt.struts.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.bjsxt.struts.dao.userDao;
import com.bjsxt.struts.db.DB;
public class userManager {
private static userManager instance = new userManager();
private static userDao userdao = null;
// public List findUser() {
// List userlist = new ArrayList();
//try{
// userlist = userdao.findUser();
// }catch(Exception e) {
// e.printStackTrace();
// }
// return userlist;
// }
public List<User> findUser() {
Connection conn = null;
List<User> userlist = new ArrayList<User>();
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from t_user where user_id <> 'root' order by user_id ";
try {
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (
SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
User user = new User();
user.setUserid(rs.getString("user_id"));
user.setUsername(rs.getString("user_name"));
user.setPassword(rs.getString("password"));
user.setTel(rs.getString("contact_tel"));
//user.setEmail(rs.getString("email"));
user.setCreatedate(rs.getDate("create_date"));
userlist.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
DB.closeRs(rs);
DB.closeStmt(stmt);
DB.closeConn(conn);
return userlist;
}
public static userManager getInstance() {
return instance;
}
public static void setInstance(userManager instance) {
userManager.instance = instance;
}
public static userDao getUserdao() {
return userdao;
}
public static void setUserdao(userDao userdao) {
userManager.userdao = userdao;
}
}
下面是处理层接口
package com.bjsxt.struts.dao;
import java.sql.Connection;
import java.util.List;
import com.bjsxt.struts.user.User;
public interface userDao {
//寻找用户
public List findUser();
}
下面是实现代码
package com.bjsxt.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;