很简单的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;
}
}
提价后
http://asirt:8080/ssh/account-add?account.name=asd
运行结果
信息: Server startup in 9030 ms
asd
null
15:14:28.187 [http-nio-8080-exec-2] ERROR org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler - Exception occurred during processing request: [java.lang.NullPointerException, null]
整个项目传百度云,我给你看看,正好刚学完ssh
看了下,这行不对,为什么ref会是Dao
如果你导入了struts-spring-plugin包的话,是不用在spring配置文件写注入的,只需要action中service的字段名和spring配置文件中ref一致并提供set方法,准确的说是按照set方法的名字来注入的
,手动注入也可以,就是上面说的那个,你配置写错了
你这个有点乱,为什么action是service,注入ref又是dao,虽然名字是不影响,不过,你没有service层吗,直接就导入dao了,可以通过注解的方式避免这种问题啊