简体   繁体   中英

Simple Spring MVC Hibernate project using Maven

I have been trying to follow this tutorial: http://viralpatel.net/blogs/2010/11/spring3-mvc-hibernate-maven-tutorial-eclipse-example.html

I did not ran into any problem using the tutorial but my question is: how can I create a war so I can deploy the app in tomcat?

Edit: I have been able to create the war and deploy it but now when trying to access localhost:8080/MavenWeb I have a 404 page.

The war is built properly and the name of it is MavenWeb.war, as specified in the finalName tag in the pom.xml file.

Here is the log of tomcat when deploying:

May 27, 2012 9:32:14 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_04\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_04\;C:\apache-tomcat-7.0.27\lib;C:\apache-maven-3.0.4\bin;C:\Program Files\Java\jdk1.7.0_04\\bin;C:\apache-tomcat-7.0.27\lib;.
May 27, 2012 9:32:14 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 27, 2012 9:32:14 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 27, 2012 9:32:14 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 523 ms
May 27, 2012 9:32:14 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 27, 2012 9:32:14 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.27
May 27, 2012 9:32:14 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive C:\apache-tomcat-7.0.27\webapps\MavenWeb.war
May 27, 2012 9:32:15 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\apache-tomcat-7.0.27\webapps\MavenWeb\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
May 27, 2012 9:32:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.27\webapps\docs
May 27, 2012 9:32:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.27\webapps\examples
May 27, 2012 9:32:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.27\webapps\host-manager
May 27, 2012 9:32:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.27\webapps\manager
May 27, 2012 9:32:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.27\webapps\ROOT
May 27, 2012 9:32:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 27, 2012 9:32:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 27, 2012 9:32:17 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2483 ms

The welcome file is properly set in the web.xml, I created the corresponding list.jsp and put it in the WEB-INF/jsp folder.

Here is the POM.xml

    <?xml version="1.0" encoding="UTF-8"?><project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>Spring3HibernateMaven</groupId>
  <artifactId>Spring3HibernateMaven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <description></description>


  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
      </plugin>
    </plugins>

    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <!-- <finalName>${artifactId}-${version}</finalName> -->
    <finalName>MavenWeb</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
  </build>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.5.1-Final</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.4.2</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.10</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>20030825.184428</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>20030825.183949</version>
    </dependency>
  </dependencies>
  <properties>
    <org.springframework.version>3.0.2.RELEASE</org.springframework.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

The war is built, I am able to deploy it into Tomcat without any error, but when trying to access it with localhost:8080/MavenWeb/ I only have a white page.

The first page is configured this way under the web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Spring3-Hibernate</display-name>
    <welcome-file-list>
        <welcome-file>list.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

So if I properly understand, when accessing localhost:8080/MavenWeb/ I should be directly redirected to list.html

Did the following on this

Installed Tomcat 6, as the source and target was 1.5 http://tomcat.apache.org/whichversion.html

Added the role manager-gui to tomcat-users.xml

  <role rolename="manager-gui"/>
  <user username="tomcat" password="tomcat" roles="manager-gui"/>

Then logged in to the http://localhost:8080/manager/ successfully deploy the Hello World Servlet, which sadly was of little use to the OP as this code associated with the tutorial was not the finished code. Meh.

I suggested going through this mvn tutorial from sonatype as that would build up the Maven skills and knowledge to the point where this first tutorial could be troubleshooted effectively.

Also suggested including references to the pom XSD in pom.xml so that tools could validate the content of same as it was developed.

From the one of sonatype tutorial pom file we have

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

Then in IntelliJ, for example, I can enter < CTRL+SPACE and get a drop down list of valid elements that part of the file.

Your war did not get deployed properly. The startup log says that servlet-api.jar has not loaded correctly. Fix that first and then we can diagnose it a little further.

This is what the log says

org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\apache-tomcat-7.0.27\webapps\MavenWeb\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

There seems to be 2 versions of servlet-api jar in your classpath and one of them is causing problems. Also you're using JDK 7 . Please check compatibility of the jars that you are using with JDK 7

I think you have servlet.jar and servlet-api-2.5.jar in your classpath. Remove servlet.jar and you should be rolling.

  1. Verify the name of the war that is getting created and deployed on tomcat. Many times maven will create war files with version numbers in it. That impacts the URL to be used.

  2. Also, I may be wrong about this, but in the web.xml, shouldn't the URL Pattern be: "/*" instead of just "/"

Do you have a typo here??

In your web.xml you have specified list.jsp as the welcome file list.

But after displaying the web.xml, you have written that the list.html is not getting redirected.

Hope this helps you.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM