[b]开发环境:[/b]
MyEclipse7+Tomcat5+MySQL5+Struts1.2+Hibernate3.2+Spring2.0
[b]相关代码:[/b]
[color=indigo]index.jsp中代码:[/color]
[color=indigo]AddAdmin.java代码:[/color]
package edu.bhu.Kupid.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import edu.bhu.Kupid.domain.Admins;
import edu.bhu.Kupid.service.AdminsService;
public class AddAdmin extends Action {
private AdminsService admServer;
public AdminsService getAdmServer() {
return admServer;
}
public void setAdmServer(AdminsService admServer) {
this.admServer = admServer;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
String loginname="admin";
String pwd="admin";
Admins adm=new Admins();
adm.setPwd(pwd);
adm.setId("1234665");
adm.setLoginname(loginname);
adm.setLevel(0);
admServer.addAdmin(adm);
return mapping.findForward("ok");
}
}
[color=indigo]AdminsService.java代码:[/color]
package edu.bhu.Kupid.service;
import java.util.List;
import edu.bhu.Kupid.domain.Admins;
public interface AdminsService {
public void addAdmin(Admins adm);
public List findAdminsByName(String name);
}
[color=indigo]AdminsServiceImpl代码:[/color]
package edu.bhu.Kupid.service;
import java.util.List;
import edu.bhu.Kupid.domain.Admins;
import edu.bhu.Kupid.persist.AdminsDao;
public class AdminsServiceImpl implements AdminsService {
private AdminsDao admDao;
public void addAdmin(Admins adm) {
admDao.insert(adm);
}
public AdminsDao getAdmDao() {
return admDao;
}
public void setAdmDao(AdminsDao admDao) {
this.admDao = admDao;
}
public List<Admins> findAdminsByName(String name) {
return null;
}
}
[color=indigo]AdminsDao.java代码:[/color]
package edu.bhu.Kupid.persist;
import edu.bhu.Kupid.domain.Admins;
public interface AdminsDao extends BaseDao{
}
[color=indigo]AdminsDaoImpl.java代码:[/color]
package edu.bhu.Kupid.persist;
import edu.bhu.Kupid.domain.Admins;
import edu.bhu.Kupid.persist.BaseDaoImpl;
public class AdminsDaoImpl extends BaseDaoImpl implements AdminsDao {
}
[color=indigo]BaseDao.java代码:[/color]
package edu.bhu.Kupid.persist;
import java.io.Serializable;
public interface BaseDao {
public void insert(T entity);
public void update(T entity);
public void delete(T entity);
public void deleteById(PK id);
public T findById(PK id);
}
[color=indigo]BaseDaoImpl.java代码:[/color]
package edu.bhu.Kupid.persist;
import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {
protected Class entityClass;
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
public void deleteById(PK id) {
getHibernateTemplate().delete(getHibernateTemplate().get(entityClass, id));
}
public T findById(PK id) {
return (T)getHibernateTemplate().get(entityClass, id);
}
public void insert(T entity) {
getHibernateTemplate().save(entity);
}
public void update(T entity) {
getHibernateTemplate().update(entity);
}
}
[color=indigo]
Admins.java代码(这个BEAN的代码是利用MyEclipse的DBExplorer的Hibernate Reverse Engineering生成的)[/color]
package edu.bhu.Kupid.domain;
/**
- Admins entity. @author MyEclipse Persistence Tools */
public class Admins implements java.io.Serializable {
// Fields
private String id;
private String loginname;
private String pwd;
private Integer level;
// Constructors
/** default constructor */
public Admins() {
}
/** full constructor */
public Admins(String id, String loginname, String pwd, Integer level) {
this.id = id;
this.loginname = loginname;
this.pwd = pwd;
this.level = level;
}
// Property accessors
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getLoginname() {
return this.loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getLevel() {
return this.level;
}
public void setLevel(Integer level) {
this.level = level;
}
}
[color=indigo]Admins.hbm.xml代码:[/color]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
[color=indigo]spring-admins.xml代码:[/color]
<?xml version="1.0" encoding="UTF-8"?>
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">
[color=indigo]
applicationContext.xml代码:[/color]
<?xml version="1.0" encoding="UTF-8"?>
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="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/configure.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>/edu/bhu/Kupid/domain/Admins.hbm.xml
</value>
<!-- 好多好多Hibernate -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="baseDao" class="edu.bhu.Kupid.persist.BaseDaoImpl" abstract="true">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
[color=indigo]struts-config.xml代码:[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
[color=indigo]web.xml代码:[/color]
<?xml version="1.0" encoding="UTF-8"?>
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
3
detail
3
0
action
.do
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
SpringContextServlet
org.springframework.web.context.ContextLoaderServlet
1
OpenSessionInView
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
singleSession
false
OpenSessionInView
.do
index.jsp
[b]程序目的:[/b]
index.jsp上的按钮被点击后,一条Admin的信息入库。
[b]
产生错误:[/b]
浏览器中错误信息:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:173)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
root cause
java.lang.NullPointerException
edu.bhu.Kupid.actions.AddAdmin.execute(AddAdmin.java:37)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:173)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.30 logs.
Apache Tomcat/5.0.30
MyEclipse中错误信息: 无。
最后感谢您的帮助。祝您好运。棘手问题郁闷中。。。
[b]问题补充:[/b]
AddAdmin第37行是:admServer.addAdmin(adm);
[b]问题补充:[/b]
我说的嘛,怎么出空指针,昨天晚上做了这样一个试验:
注释掉原来37行的代码,然后手工创建spring上下文,就能通过。果然是SSH整合中有错误。
//admServer.addAdmin(adm);
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
AdminsService admServer=(AdminsService)ctx.getBean("adminsServer");
admServer.addAdmin(adm);
我是第一次做SSH,不太会弄,请各位多指教。
现在还有一个很头疼的地方,就是struts,我用了tiles来做页面的分帧,于是给struts-config.xml文件加入了
结果这段代码一加进来,jsp提交表单到action的时候就找不到action了,浏览器报404异常。
PS:回lewhwa :因为有一次我发项目,有个仁兄说下载太麻烦,不如直接贴代码的好,于是我就贴了代码。。。