struts2登陆问题·急~~~~~~只有这么点分了T.T

我已经被这个苦恼的问题 折磨好半天了。。高手们帮我看看问题出在哪了  总是空指针异常 可是我检查了很仔细了还是没看出来哪里不对。急~~~~
登陆页面

  <tr>
        <td width="70" align="center">用户名:</td>
        <td><input name="user.username" type="text" class="id" style="width:160px;"/></td>
      </tr>
      <tr>
        <td width="70" align="center">密&nbsp;&nbsp;码:</td>
        <td><input name="user.password" type="password" class="id" style="width:160px;"/></td>
      </tr>

 

UserDAO

public class UserDAO {
    private JdbcTemplate jt;

    public JdbcTemplate getJt() {
        return jt;
    }

    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    
    public User getUser(User user){
        String sql="select count(*) from user where username='"
            + user.getUsername() + "' and password='" + user.getPassword()
            + "'";
        
        int tt=jt.queryForInt(sql);
        if(tt>0)
            return user;    
        else
            return null;
    }

 UserService

public class UserService {
     private UserDAO userDAO;

    public UserDAO getUserDAO() {
        return userDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public boolean isLogin(User user){
        
        if(userDAO.getUser(user)!=null){    
            return true;
        }
        return false;
    }

 

UserAction

public class UserAction extends ActionSupport {
    
    private UserService userService;
    private User user;
        public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
        public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
public String execute() throws Exception{
        
        if(userService.isLogin(user)){
            ActionContext context = ActionContext.getContext();
            Map session = context.getSession();
            session.put("user", user);
            System.out.println("判断ACTION已经执行");
            return "success";
            
         }
        else
        {
                        return "input";
        }

        }

 

在你的User中加入一个 空的构造方法试试。
[code="java"]
public User(){}[/code]

[quote] 总是空指针异常[/quote]

具体在哪一行?

你的userService没有看到实例化呀?是Spring注入的吗?

看看具体的错误堆栈。

User中有set方法吧?

UserDao中JdbcTemplate在spring的配置文件中配置好。

[code="xml"][/code]

是不是你的User类没有实现ModelDiver接口呢?

具体的错误堆栈,贴出来看看。

具体那个地方报错////~~启动tomcat的调试模式调式。

UserDAO 注入到 Service中没?

[quote]select count(*) from user where username='"

14. + user.getUsername() + "' and password='" + user.getPassword() [/quote]

在这句后面打印出user.getUsername()和user.getPassword();

如果打印出来,在sql命令行执行:select count(*) from user where username='username' and password='password' (替换username和password)

还有一种可能spring容器没有初始化。web.xml配置好。
[code="xml"]

org.springframework.web.context.ContextLoaderListener

[/code]

单步调试吧,最可靠的办法。

JdbcTemplate的使用需要有DataSource的支持

参看详细介绍jdbcTemplate的详细使用
http://blog.csdn.net/nomads/archive/2006/05/05/709551.aspx

在 action service dao 中 依次debug 看看 到底哪出了问题

[quote]我尝试你的办法了 但是目前 我的疑虑是 登陆页面的username和password没有存进user类中 感觉是这样呢
[/quote]
如果你感觉是这样,你需要在UserAction 中测试user的username和password值,
如果这里都是空的话,有必要仔细查看一下你的action的配置文件了。

你先看一下你的值传到action中去了没有,用下面的代码我测试了,没有问题。我刚才说了可能是没有实现ModelDriven接口原因

[code="java"] public class UserAction extends ActionSupport implements ModelDriven{

private User user = new User();

public String execute() {
    System.out.println("name=" + user.getName());
    System.out.println("age=" + user.getAge());
    return SUCCESS;
}

@Override
public User getModel() {
    return user;
}

}
[/code]

[quote]谢谢你的解决方案~~ [/quote]
不客气~~ :D :D