SSH2整合中老是出现 java.lang.NullPointerException ,DAO未注入
SSH2整合中老是出现 java.lang.NullPointerException ,DAO未注入的情况,我查不少资料但还是没能解决,望各位大侠帮帮忙POJO文件
package org.lxh.ssh2.pojo;
public class News implements java.io.Serializable {
private Integer nid;
private String title;
private String content;
public News() {
}
public News(String title, String content) {
this.title = title;
this.content = content;
}
public Integer getNid() {
return this.nid;
}
public void setNid(Integer nid) {
this.nid = nid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
}
DAO接口
package org.lxh.ssh2.dao;
import java.util.List;
import org.lxh.ssh2.pojo.News;
public inte易做图ce INewsDAO {
public void doCreate(News news) throws Exception;
public void doRemove(News news) throws Exception;
public void doUpdate(News news) throws Exception;
public List<News> findAll() throws Exception;
public News findById(Integer id) throws Exception;
}
DAO实现
package org.lxh.ssh2.dao.impl;
import java.util.List;
import org.lxh.ssh2.dao.INewsDAO;
import org.lxh.ssh2.pojo.News;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class NewsDAOImpl extends HibernateDaoSupport implements INewsDAO {
public void doCreate(News news) throws Exception {
super.getHibernateTemplate().save(news);
}
public void doRemove(News news) throws Exception {
super.getHibernateTemplate().delete(news);
}
public void doUpdate(News news) throws Exception {
super.getHibernateTemplate().update(news);
}
@SuppressWarnings("unchecked")
public List<News> findAll() throws Exception {
String hql="from News news";
return (List<News>)super.getHibernateTemplate().find(hql);
}
public News findById(Integer id) throws Exception {
News news=(News)super.getHibernateTemplate().get(News.class, id);
return news;
}
}
action实现(只做了一个表查询的操作)
package org.lxh.ssh2.action;
import java.util.List;
import org.lxh.ssh2.dao.INewsDAO;
import org.lxh.ssh2.pojo.News;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class NewsAction extends ActionSupport{
private News news=null;
private INewsDAO dao=null;
public String findAll() throws Exception{
List<News> n=this.dao.findAll();
ActionContext.getContext().getSession().put("news",n);
return SUCCESS;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public void setDao(INewsDAO dao) {
this.dao = dao;
}
}
下面是我的配置文件
applicationContext-dataSource.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mldn">
</property>
<property name="username" value="sa"></property>
<property name="password" value="123"></property>
</bean>
</beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="inewsdao" class="org.lxh.ssh2.dao.INewsDAO" abstract="true"/>
<bean id="newsdaoimpl" class="org.lxh.ssh2.dao.impl.NewsDAOImpl" parent="inewsdao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
</beans>
applicationContext-struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="newsAction" class="org.lxh.ssh2.action.NewsAction">
<property name="dao">
<ref bean="newsdaoimpl"/>
</property>
</bean>
</beans>
struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="news" namespace="/" extends="struts-default">
<action name="news_*" class="org.lxh.ssh2.action.NewsAction" method="{1}">
<result name="SUCCESS" type="dispatcher">news.jsp</result>
</action>
</package>
</struts>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
希望各位指教,谢谢! --------------------编程问答-------------------- <bean id="inewsdao" class="org.lxh.ssh2.dao.INewsDAO" abstract="true"/>
把id换一下,试一下!,别和类名一样! --------------------编程问答-------------------- 谢谢热情回答,还是不行 --------------------编程问答-------------------- spring分层体系是这样的。。。applicationContext.xml文件里最底层是 dataSource, 然后是sessionFactory。。。然后是 DAOImpl 然后是ServiceImpl (一般service后面还有声明式事务管理) 最后才是注入 action。。。这样是层层的依赖注入的。。。楼主你都没有为这个dataSource配置 sessionFactory。。。。这可能是你报异常的原因 --------------------编程问答-------------------- 不好意思!!有的,忘粘贴了
applicationContext-hibernate.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/lxh/ssh2/pojo/News.hbm.xml</value></list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans> --------------------编程问答--------------------
朋友你好!根据你说的改了后是找不到Action了错误如下:
exception
Unable to instantiate Action, org.lxh.ssh2.action.NewsAction, defined for 'news_findAll' in namespace '/'Failed to convert property value of type [org.lxh.ssh2.action.NewsAction] to required type [org.lxh.ssh2.pojo.News] for property 'news'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.lxh.ssh2.action.NewsAction] to required type [org.lxh.ssh2.pojo.News] for property 'news': no matching editors or conversion strategy found
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:307)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
root cause
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.lxh.ssh2.action.NewsAction] to required type [org.lxh.ssh2.pojo.News] for property 'news'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.lxh.ssh2.action.NewsAction] to required type [org.lxh.ssh2.pojo.News] for property 'news': no matching editors or conversion strategy found
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:385)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:274)
com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:182)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:162)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:133)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
root cause
java.lang.IllegalArgumentException: Cannot convert value of type [org.lxh.ssh2.action.NewsAction] to required type [org.lxh.ssh2.pojo.News] for property 'news': no matching editors or conversion strategy found
org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:380)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:274)
com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:182)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:162)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:133)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
补充:Java , Web 开发