日期:2014-05-19  浏览次数:20678 次

S2SH下空指针问题 求助
S2SH整合 做个登陆搜索系统。一直有个空指针错误 不知道怎么解决 好像是注入的问题 但是不知道问题在哪里
Java code

package action;


import dao.AccountDAO;
import model.Account;

public class LoginAction{
    private Account account;
    private AccountDAO accountDao;

    
    public void setAccount(Account account)
    {
        this.account=account;
    }
    public Account getAccount ()
    {
        return account;
    }
    public void setAccountDao(AccountDAO accountDao)
    {
        this.accountDao = accountDao;
    }
    public AccountDAO getAccountDao()
    {
        return this.accountDao;
    }
    
    public String execute() 
    {
        System.out.print(accountDao.toString());
/报错         System.out.print([b]accountDao[/b].toString()+"name:"+account.getName()+"password:"+account.getPassword());
        if ([b]accountDao[/b].isValid(account.getName(),account.getPassword())==true) return "success";
        else return "fail";
    }
}


Java code

在AccountDAO中我自己实现的代码
public boolean isValid(final String name,final String password) {
        [b]Session session=getHibernateTemplate().getSessionFactory().openSession();[/b]
        Criteria tmp=session.createCriteria(Account.class);
        List result = tmp.add(Restrictions.eq("name", name))
                         .add(Restrictions.eq("password", password))
                         .list();
        if(result.size()>0) {
            return true;
        } else {
            return false;
        }    
    }


Java code

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/large_experiment">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>model/Person.hbm.xml</value>
                <value>model/Account.hbm.xml</value></list>
        </property></bean>
    <bean id="PersonDAO" class="dao.PersonDAO">
        <property name="sessionFactory">