zhbAdnim 2016-10-19 07:27 采纳率: 0%
浏览 1404
已结题

搭建简单SSH框架注入问题

很简单的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&amp;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;
}

}

  • 写回答

4条回答 默认 最新

  • zhbAdnim 2016-10-19 07:35
    关注

    提价后
    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]

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名