最近在Myeclipse下学习了使用maven管理struts、hibernate项目,做了一个简单的注册功能,过程那个让人郁闷呐。
简单说一下问题,添加maven、struts、hibernate都是通过myeclipse自动完成的,如果第一次通过myeclipse部署到本地server,完全没有问题;可是想生成war包,部署到远程server上,问题就来了。
clean, install,
部署,访问,哎,报错了
首先出现的是这样的错误:
java.lang.NullPointerException at my.hibernate.pojo.UserDAO.findUser(UserDAO.java:22) at com.demo.struts.action.LogonAction.execute(LogonAction.java:45) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source)
找了大半天,查各种资料,都没有解决,最后都快绝望了,无目的地到各目录下面,突然发现怎么少了个配置文件,原来是hibernate的表映射文件(*.hbm.xml)在打包的时候没有被包含进去。
手动从源码中把这个文件放在myeclipse中src/main/webapp/WEB-INF/classes/相应的包 下,重新打包,部署,检查表映射文件已经被包含进去了,好,访问,
又出现了第二个错误No result defined for action,有了上次经历,先检查配置文件,果然,struts的配置文件又不见了,手动添加重新部署,这次所有配置文件都已经被包含在war包中,访问成功。
我发现每次clean之后classes下面的配置文件都会被清理掉,再次install的时候,就不会被包含在war包中,但是总不能每次clean之后都手动添加配置文件吧,通过查看pom.xml文件的说明,我发现可以通过pom指定每次install所需的资源文件,被指定的文件会在install时被包含在war包中。以下是我的pom.xml文件中指定资源的部分:
1 <project 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 3 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>yst</groupId> 6 <artifactId>yst</artifactId> 7 <version>1</version> 8 <packaging>war</packaging> 9 <name>yst</name> 10 <description></description> 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 </properties> 15 16 <dependencies> 17 </dependencies> 18 19 <build> 20 <defaultGoal>install</defaultGoal> 21 <directory>D:/Ma/target</directory> 22 <finalName>yst</finalName> 23 <resources> 24 <resource> 25 <directory>src/main/java/tju/yst/pojo/user</directory> 26 <includes> 27 <include>Usert.hbm.xml</include> 28 </includes> 29 <targetPath>tju/yst/pojo/user</targetPath> 30 </resource> 31 <resource> 32 <directory>src/main/java/</directory> 33 <includes> 34 <include>*.xml</include> 35 </includes> 36 <targetPath></targetPath> 37 </resource> 38 </resources> 39 <plugins> 40 <plugin> 41 <artifactId>maven-war-plugin</artifactId> 42 </plugin> 43 <plugin> 44 <artifactId>maven-compiler-plugin</artifactId> 45 <configuration> 46 <source>1.6</source> 47 <target>1.6</target> 48 </configuration> 49 </plugin> 50 </plugins> 51 </build> 52 </project>
再次clean、install,无需手动添加配置文件,打包成功。