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 interface 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>
希望各位指教,谢谢!