本人新人一枚,在学springmvc,报空指针异常能帮忙看看嘛
public class LoginController extends SimpleFormController{
private String showUser;
private String errPage;
public String getErrPage() {
return errPage;
}
public void setErrPage(String errPage) {
this.errPage = errPage;
}
public LoginController(){
setCommandClass(User.class);
}
protected ModelAndView onSubmit(Object command,BindException exception)throws Exception{
User user = (User) command;
LoginDao log = new LoginDao();
if(user.getUser().equals("")){
return new ModelAndView(getErrPage(),"error","用户名不能为空!");
}
if(user.getPwd().equals("")){
return new ModelAndView(errPage,"error","密码不能为空!");
}
if(user.getPwd().length()<6){
return new ModelAndView(errPage,"error","密码不能少于6位!");
}
if(log.isExist(user.getUser(),user.getPwd())){
return new ModelAndView(getShowUser(),"user",user);
}
return new ModelAndView(errPage,"error","账号或密码错误!");
}
public class LoginDao {
public boolean isExist(String username,String password){
Connection conn=DBManager.getConn();
boolean result=false;
String sql="select * from login where name=? and password=?";
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next()){
result=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(rs, ps, conn);
}
return result;
}
}
前面几个if判断都能执行并跳转但一运行到这个方法就报错
该方法在测试类中是可以运行的
public class Test {
public static void main(String[] args) {
LoginDao log = new LoginDao();
System.out.println(log.isExist("zhangsan","123456"));
}
}是不是还要找spring-servlet中配置?还是别的
public class LoginController extends SimpleFormController{
private String showUser;
private String errPage;
public String getErrPage() {
return errPage;
}
public void setErrPage(String errPage) {
this.errPage = errPage;
}
public LoginController(){
setCommandClass(User.class);
}
protected ModelAndView onSubmit(Object command,BindException exception)throws Exception{
User user = (User) command;
LoginDao log = new LoginDao();
if(user.getUser().equals("")){
return new ModelAndView(getErrPage(),"error","用户名不能为空!");
}
if(user.getPwd().equals("")){
return new ModelAndView(errPage,"error","密码不能为空!");
}
if(user.getPwd().length()<6){
return new ModelAndView(errPage,"error","密码不能少于6位!");
}
if(log.isExist(user.getUser(),user.getPwd())){
return new ModelAndView(getShowUser(),"user",user);
}
return new ModelAndView(errPage,"error","账号或密码错误!");
}
public class LoginDao {
public boolean isExist(String username,String password){
Connection conn=DBManager.getConn();
boolean result=false;
String sql="select * from login where name=? and password=?";
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next()){
result=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(rs, ps, conn);
}
return result;
}
}
前面几个if判断都能执行并跳转但一运行到这个方法就报错
该方法在测试类中是可以运行的
public class Test {
public static void main(String[] args) {
LoginDao log = new LoginDao();
System.out.println(log.isExist("zhangsan","123456"));
}
}是不是还要找spring-servlet中配置?还是别的
解决方案 »
- 通过传过来value的值让select下的option默认选中!不能用jquery
- 对异步刷新获取的数据排序、分页
- jboss的连接池出现null值的问题
- 做个无限级别类,想把数据库里的数据排成树型。该如何排序?
- 一个关于session的安全问题
- 用servlet显示报表的问题
- 初接触ASP,没有基础。十天左右学习上手?请高手支招,谢谢!
- 请问怎样把orion集成到eclipse里面? 哪里有这个插件下?谢谢
- 我写了一个从DATASOURCE取得CONNECTION的工具类,大家看看
- 紧急征apachemodulejserv.dll
- mybatis 打印Preparing : sql select ............. where id = ?时,是已经获得了连接还是没有拿连接
- shiro jsession 每次都会产生新会话.
DBManager.getCon();贴出来就知道了
要放到工程的lib下面
if(rs.next()){
result=true;
}
问题应该出在这句上,执行executeQuery()方法的时候sql没有查到数组,导致rs.next()方法取下一条数据的时候报出了空指针。