一个用hibernate整合struts2的操作数据库的程序,底层是用serviceFactory生成业务逻辑实现类对象,操作数据库的,下面还有DAO接口什么的,不过这些都没问题,我写过测试类,可以操作数据库了。然后又加上了struts2的表现层,再用MyEclipse运行程序时,启动tomcat加载后工程报出空指针异常,错误如下:
警告: Error during context [/struts2+hibernate3.0] restart
java.lang.NullPointerException
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1097)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
at java.lang.Thread.run(Thread.java:662)
我用的是tomcat6.0编译的。配置文件如下:
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="struts2" extends="struts-default">
<action name="showAll" class="action.ShowAllAction">
<result name="success">/showAll.jsp</result>
</action>
<action name="add" class="action.AddAction">
<result name="success" type="redirect">showAll.action</result>
</action>
<action name="delete" class="action.deleteAction">
<result name="success" type="redirect">showAll.action</result>
</action>
<action name="update" class="action.UpdateAction">
<result name="success" type="redirect">showAll.action</result>
</action>
</package>
</struts> web.xml:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>showAll.action</welcome-file>
</welcome-file-list>
Action程序如下:
1)showAllAction:
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import service.ServiceFactory;
import service.ProductService;
public class ShowAllAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
List list=ps.queryAllProduct();
ServletActionContext.getRequest().setAttribute("all",list);
return SUCCESS;
}
}
2)AddAction:
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import service.ServiceFactory;
import service.ProductService;
public class ShowAllAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
List list=ps.queryAllProduct();
ServletActionContext.getRequest().setAttribute("all",list);
return SUCCESS;
}
}
3)UpdateAction:
package action;
import service.ServiceFactory;
import service.ProductService;
import bean.Product;
import com.opensymphony.xwork2.ActionSupport;
public class UpdateAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private Double price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String execute()throws Exception
{
ProductService ps=ServiceFactory.getServiceInstence();
Product p=new Product();
p.setId(id);
p.setName(name);
p.setPrice(price);
ps.updateProduct(p);
return SUCCESS;
}
}
4)deleteAction:
package action;
import com.opensymphony.xwork2.ActionSupport;
import service.ServiceFactory;
import service.ProductService;
//import bean.Product;
public class deleteAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L;
private String id; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
ps.deleteProduct(id);
return SUCCESS;
}
}
还有三个jsp文件:
1)add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<center>
<h2>添加产品</h2>
<form action="add">
产品id<input type="text" name="id"/><br>
产品名<input type="text" name="name"/><br>
产品价格<input type="text" name="price"/><br/>
<input type="submit" value="提交"/><br>
<input type="reset" value="重置"/><br>
</form>
</center>
</body>
</html>
2)showAll.jsp:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>产品列表</title>
</head>
<body>
<center>
<h2>产品列表</h2>
<table border="1">
<tr>
<td>产品ID</td>
<td>产品名称</td>
<td>产品价格</td>
<td>是否删除</td>
<td>是否更新</td>
</tr>
<s:iterator value="#request.all" id="product">
<tr>
<td><s:property value="#product.id"/></td>
<td><s:property value="#product.name"/></td>
<td><s:property value="#product.price"/></td>
<td><a href="delete.action?id=<s:property value='#product.id'/>">是否删除</a></td>
<td><a href="update.jsp?id=<s:property value='#product.id'/>">更新</a></td>
</tr>
</s:iterator>
</table>
<a href="add.jsp"></a>
</center>
</body>
</html>
3)update.jsp:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<center>
<h2>更新产品</h2>
</center>
<s:form action="update">
<s:textfield label="产品ID" name="id"></s:textfield>
<s:textfield label="产品名" name="name"></s:textfield>
<s:textfield label="产品价格" name="price"></s:textfield>
<s:submit value="更新"></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
</body>
</html>
请求各位哥哥伸出援助之手哈,下周天就要交作业了,55~
警告: Error during context [/struts2+hibernate3.0] restart
java.lang.NullPointerException
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1097)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
at java.lang.Thread.run(Thread.java:662)
我用的是tomcat6.0编译的。配置文件如下:
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="struts2" extends="struts-default">
<action name="showAll" class="action.ShowAllAction">
<result name="success">/showAll.jsp</result>
</action>
<action name="add" class="action.AddAction">
<result name="success" type="redirect">showAll.action</result>
</action>
<action name="delete" class="action.deleteAction">
<result name="success" type="redirect">showAll.action</result>
</action>
<action name="update" class="action.UpdateAction">
<result name="success" type="redirect">showAll.action</result>
</action>
</package>
</struts> web.xml:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>showAll.action</welcome-file>
</welcome-file-list>
Action程序如下:
1)showAllAction:
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import service.ServiceFactory;
import service.ProductService;
public class ShowAllAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
List list=ps.queryAllProduct();
ServletActionContext.getRequest().setAttribute("all",list);
return SUCCESS;
}
}
2)AddAction:
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import service.ServiceFactory;
import service.ProductService;
public class ShowAllAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
List list=ps.queryAllProduct();
ServletActionContext.getRequest().setAttribute("all",list);
return SUCCESS;
}
}
3)UpdateAction:
package action;
import service.ServiceFactory;
import service.ProductService;
import bean.Product;
import com.opensymphony.xwork2.ActionSupport;
public class UpdateAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private Double price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String execute()throws Exception
{
ProductService ps=ServiceFactory.getServiceInstence();
Product p=new Product();
p.setId(id);
p.setName(name);
p.setPrice(price);
ps.updateProduct(p);
return SUCCESS;
}
}
4)deleteAction:
package action;
import com.opensymphony.xwork2.ActionSupport;
import service.ServiceFactory;
import service.ProductService;
//import bean.Product;
public class deleteAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L;
private String id; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String execute()throws Exception
{ ProductService ps=ServiceFactory.getServiceInstence();
ps.deleteProduct(id);
return SUCCESS;
}
}
还有三个jsp文件:
1)add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<center>
<h2>添加产品</h2>
<form action="add">
产品id<input type="text" name="id"/><br>
产品名<input type="text" name="name"/><br>
产品价格<input type="text" name="price"/><br/>
<input type="submit" value="提交"/><br>
<input type="reset" value="重置"/><br>
</form>
</center>
</body>
</html>
2)showAll.jsp:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>产品列表</title>
</head>
<body>
<center>
<h2>产品列表</h2>
<table border="1">
<tr>
<td>产品ID</td>
<td>产品名称</td>
<td>产品价格</td>
<td>是否删除</td>
<td>是否更新</td>
</tr>
<s:iterator value="#request.all" id="product">
<tr>
<td><s:property value="#product.id"/></td>
<td><s:property value="#product.name"/></td>
<td><s:property value="#product.price"/></td>
<td><a href="delete.action?id=<s:property value='#product.id'/>">是否删除</a></td>
<td><a href="update.jsp?id=<s:property value='#product.id'/>">更新</a></td>
</tr>
</s:iterator>
</table>
<a href="add.jsp"></a>
</center>
</body>
</html>
3)update.jsp:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<center>
<h2>更新产品</h2>
</center>
<s:form action="update">
<s:textfield label="产品ID" name="id"></s:textfield>
<s:textfield label="产品名" name="name"></s:textfield>
<s:textfield label="产品价格" name="price"></s:textfield>
<s:submit value="更新"></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
</body>
</html>
请求各位哥哥伸出援助之手哈,下周天就要交作业了,55~
tomcat还能编译吗?只是服务器吧,你是用Myeclipse编译的 然后发布到tomcat还有启动的时候报错 说明配置文件有问题
我刚看了你的2个配置文件..没啥大问题.你把程序单独拉出来在tomcat跑一下吧。。有可能是eclipse问题