action调用其他模型的业务层,报空指针问题请教

CoderLim 2017-03-10 12:49:56
SSH环境
在打开添加员工页面的时候需要先加载一个部门选择列表,我的想法是点击添加员工按钮时,调用员工action里的一个input方法跳转到添加页面,在input方法里,先使用部门模块的service层查询部门表,将部门表封装进一个list返回到页面。因此在员工action注入了部门的ebi,也就是这个ebi报的空指针异常。
部门模块的增删改查都已经完成了,配置文件也没有问题,员工模块的配置也完成了,登录功能什么的没有问题。哪位高手帮忙看看是哪里出的问题导致没有注入成功呢?

我的代码如下:

EmpAction

//注入业务层接口
private EmpEbi empEbi;
public void setEmpEbi(EmpEbi empEbi) {
this.empEbi = empEbi;
}
private DeptEbi deptEbi;
public void setDeptEbi(DeptEbi deptEbi) {
this.deptEbi = deptEbi;
}

public String input(){
// 从数据库读取部门列表,加载部门选项
List<DeptModel> deptList = deptEbi.getAll(); // 这行的deptEbi报空指针
ActionContext.getContext().put("deptList", deptList);
return "input";
}


applicationContext-dept.xml

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

<!-- 配置Action -->
<bean id = "deptAction" class="cn.lim.erp.auth.dept.web.DeptAction" scope="prototype">
<!-- 注入sessionFactory -->
<property name="deptEbi" ref="deptEbi"></property>
</bean>


<!-- 配置Ebi -->
<bean id = "deptEbi" class="cn.lim.erp.auth.dept.business.ebo.DeptEbo">
<!-- 注入sessionFactory -->
<property name="deptDao" ref="deptDao"></property>
</bean>


<!-- 配置Dao -->
<bean id = "deptDao" class="cn.lim.erp.auth.dept.dao.impl.DeptImpl">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

</beans>



applicationContext-emp.xml

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

<!-- 配置Action -->
<bean id = "empAction" class="cn.lim.erp.auth.emp.web.EmpAction" scope="prototype">
<!-- 注入sessionFactory -->
<property name="empEbi" ref="empEbi"></property>
</bean>


<!-- 配置Ebi -->
<bean id = "empEbi" class="cn.lim.erp.auth.emp.business.ebo.EmpEbo">
<!-- 注入sessionFactory -->
<property name="empDao" ref="empDao"></property>
</bean>

<!-- 配置Dao -->
<bean id = "empDao" class="cn.lim.erp.auth.emp.dao.impl.EmpImpl">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

</beans>


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" >

<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="mappingLocations">
<value>classpath:cn/lim/erp/**/vo/*Model.hbm.xml</value>
</property>
</bean>


<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!-- 给事务注入hibernate的连接池 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 注解事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/erpdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

</beans>



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<!-- Struts2加载 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Spring监听器 -->
<listener>
<!-- 监听器默认加载的是WEB-INF/applicationContext.xml -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 指定Spring框架的配置文件所在的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>


</web-app>

...全文
164 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
写get/set方法了没
我爱娃哈哈 2017-03-10
  • 打赏
  • 举报
回复
额,配置文件有哦
我爱娃哈哈 2017-03-10
  • 打赏
  • 举报
回复
注入的注解呢
CoderLim 2017-03-10
  • 打赏
  • 举报
回复
引用 11 楼 qnmdcsdn 的回复:

<!-- 配置Action -->
        <bean id = "empAction" class="cn.lim.erp.auth.emp.web.EmpAction" scope="prototype">
            <!-- 注入sessionFactory -->
            <property name="empEbi" ref="empEbi"></property>
            <property name="deptEbi" ref="deptEbi"></property>
        </bean>
啊!这么低级的错误上纠缠了半天,十分感谢!
ma_arr 2017-03-10
  • 打赏
  • 举报
回复
抓一下异常信息: try{ }catch(Exception e){ e.printStackTrace();//打印一下你堆栈信息,应该就可以一目了然了 }
  • 打赏
  • 举报
回复

<!-- 配置Action -->
        <bean id = "empAction" class="cn.lim.erp.auth.emp.web.EmpAction" scope="prototype">
            <!-- 注入sessionFactory -->
            <property name="empEbi" ref="empEbi"></property>
            <property name="deptEbi" ref="deptEbi"></property>
        </bean>
CoderLim 2017-03-10
  • 打赏
  • 举报
回复
引用 8 楼 qnmdcsdn 的回复:
看了下配置文件,你EmpAction里只注入了emp没注入dep
xml里同时加载了三个spring的配置,applicationContext-dept.xml,里注入了那个bean,这样不行吗? <param-value>classpath:applicationContext*.xml</param-value> 我刚才试了下在emp配置里面加上dep的配置,问题还是一模一样。
CoderLim 2017-03-10
  • 打赏
  • 举报
回复
引用 7 楼 ma_arr 的回复:
List<DeptModel> deptList = deptEbi.getAll(); // 这行的deptEbi报空指针 这不是你写的? 你到底会不会debug?值得怀疑, 你进入该方法里面去打个断点
我当然在业务层方法里打的断点,直接就运行出错了,程序没有中断 然后,我在这行List<DeptModel> deptList = deptEbi.getAll();上打了断点,F5Jumpinto就报空指针了
  • 打赏
  • 举报
回复
看了下配置文件,你EmpAction里只注入了emp没注入dep
ma_arr 2017-03-10
  • 打赏
  • 举报
回复
List<DeptModel> deptList = deptEbi.getAll(); // 这行的deptEbi报空指针 这不是你写的? 你到底会不会debug?值得怀疑, 你进入该方法里面去打个断点
CoderLim 2017-03-10
  • 打赏
  • 举报
回复
就只有这个对象的有问题,我调用自己的empEbi就完全没问题。不知道从A类的action访问b类的service是不是有什么特别要注意的?
CoderLim 2017-03-10
  • 打赏
  • 举报
回复
引用 4 楼 ma_arr 的回复:
deptEbi.getAll(); 检查DAO,看看是否正确执行sql 最好贴出来看看
我打了断点,在action就报异常了,都还没有走到dao
ma_arr 2017-03-10
  • 打赏
  • 举报
回复
deptEbi.getAll(); 检查DAO,看看是否正确执行sql 最好贴出来看看

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧