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

empty0.0 empty0.0 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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>

...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
empty0.0
Web 开发 81,107 成员 341,720 内容