很简单的ssh框架整合,搭建好后项目部署没有出错,运行后尝试添加一条数据出现
java.lang.NullPointerException, null
spring没有帮我注入,导致accountAction 的accountService为空
找了好久都没有找到问题的根源 以下代码
IAccount.java【接口】
public interface IAccount {
public boolean saveAccount(account account);
}
accountDAO【实现类】
package org.asirt.dao.imlp;
import org.asirt.dao.IAccount;
import org.asirt.entity.account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class accountDAO implements IAccount{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public boolean saveAccount(account account) {
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
session.save(account);
ts.commit();
session.close();
return true;
}
}
struts.xml 【配置文件】
<action name="account-go_add" class="org.asirt.action.accountAction" method="go_add">
<result name="go_add">add.html</result>
</action>
<action name="account-add" class="org.asirt.action.accountAction" method="add">
<result name="success">success.html</result>
</action>
</package>
applicationContext.xml【spring配置文件】
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.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/asirt_bbs?useUnicode=true&characterEncoding=utf-8">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/asirt/entity/account.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="accountDAO" class="org.asirt.dao.imlp.accountDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="accountAction" class="org.asirt.action.accountAction">
<property name="accountService" ref="accountDAO"></property>
</bean>
accountAction.java【Action】
package org.asirt.action;
import org.apache.catalina.User;
import org.asirt.dao.IAccount;
import org.asirt.entity.account;
import com.opensymphony.xwork2.ActionSupport;
public class accountAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -453090639825021968L;
private account account;
private IAccount accountService;
public String go_add() throws Exception{
return"go_add";
}
public String add() throws Exception{
System.out.println(account.getName());
System.out.println(getAccountService());
if(account!=null){
getAccountService().saveAccount(account);
System.out.println("hah");
}
return"success";
}
public account getAccount() {
return account;
}
public void setAccount(account account) {
this.account = account;
}
public IAccount getAccountService() {
return accountService;
}
public void setAccountService(IAccount accountService) {
this.accountService = accountService;
}
}