先发代码:
web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>applicationContext.xml: <bean name="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean name="testDao" class="com.lxf.dao.TestDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean name="/test" class="com.lxf.struts.action.TestAction">
<property name="testDao">
<ref bean="testDao"/>
</property>
</bean>
struts-config: <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
TestAction: TestDao testDao;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
System.out.println(testDao);
testDao.getAllBook();
return null;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public TestDao getTestDao() {
return testDao;
}
对于大家来说这代码应该很简单了,现在我的问题是在TestAction里面testDao为空,不知道哪里错了或者漏写了什么,请大家指正下 我以为是没有读到applicationContext.xml 所以我在两个地方写了,tomcat启动也没报错
web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>applicationContext.xml: <bean name="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean name="testDao" class="com.lxf.dao.TestDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean name="/test" class="com.lxf.struts.action.TestAction">
<property name="testDao">
<ref bean="testDao"/>
</property>
</bean>
struts-config: <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
TestAction: TestDao testDao;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
System.out.println(testDao);
testDao.getAllBook();
return null;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public TestDao getTestDao() {
return testDao;
}
对于大家来说这代码应该很简单了,现在我的问题是在TestAction里面testDao为空,不知道哪里错了或者漏写了什么,请大家指正下 我以为是没有读到applicationContext.xml 所以我在两个地方写了,tomcat启动也没报错
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货