【发布时间】:2018-10-04 12:51:10
【问题描述】:
我用 spring 和 hibernate 创建了一个 maven 项目。我在这个项目中配置了tomcat 8.5。
当我第一次尝试运行tomcat服务器时http://localhost:8080/不起作用。 google了一下,发现需要在tomcat中做一些配置:
步骤:
- 在 Tomcat 服务器上单击右键> 属性
- 在常规菜单中:单击切换位置
- 位置已从 [workspace metadata] 更改为 /Server/Tomcat v8.5 Server at localhost.server
在这个配置之后,我再次启动了tomcat服务器,http://localhost:8080/现在正在工作。我可以看到 Tomcat 欢迎页面
但是当我尝试访问http://localhost:8080/EventryApp/hello.jsp 时,它不起作用。
错误是:
HTTP Status 404 – Not Found
在日志文件中是:
127.0.0.1 - - [04/Oct/2018:09:42:30 -0300] "GET /EventryApp/hello.jsp HTTP/1.1" 404 1098
你能帮我理解这个问题吗?
按照我的设置文件:
EventryApp/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.eventryapp</groupId>
<artifactId>EventryApp</artifactId>
<version>1.0.0-CI-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EventryApp Maven Webapp</name>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.2.0.RELEASE</spring.framework.version>
<hibernate.version>4.1.9.Final</hibernate.version>
<sl4j.version>1.7.2</sl4j.version>
<junit.version>4.11</junit.version>
<hsqldb.version>2.2.9</hsqldb.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<sourceIncludes>
<sourceInclude>**/*.*</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
<!-- Configuration which allows JUnit tests to be placed in the same folder as java files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
</configuration>
</plugin>
</plugins>
<!-- Configuration which allows configuration files (such as xml files) to be placed in the same folder as java files -->
<resources>
<resource>
<directory>${basedir}/src/main/java/</directory>
</resource>
</resources>
</build>
<dependencies>
<!-- **********************************************************************
** SPRING DEPENDENCIES **
********************************************************************** -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!-- **********************************************************************
** HIBERNATE DEPENDENCIES **
********************************************************************** -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- **********************************************************************
** OTHER DEPENDENCIES **
********************************************************************** -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- **********************************************************************
** TEST DEPENDENCIES
** ********************************************************************** -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
</dependency>
</dependencies>
EventryApp/src/main/webapp/WEB-INF/web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>ApplicationContext</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
EventryApp/src/main/webapp/WEB-INF/applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable autowire -->
<context:annotation-config />
<context:component-scan base-package="com" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- If you use MySQL Database comment out this bean and let others commented -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/eventrydb" > </property>
<property name="username" value="root" ></property>
<property name="password" value="" ></property>
</bean>
<!-- SQL Server 2012 and 2014 Database drivers and connectivity code -->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" ></property>
<property name="url" value="jdbc:sqlserver://localhost;databaseName=mydb" ></property>
<property name="username" value="sa" ></property>
<property name="password" value="abcd@1234" ></property>
</bean> -->
<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" ></property>
<property name="packagesToScan" value="com.entities" ></property>
<property name="hibernateProperties">
<props>
<!-- SQL Dialect -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- SQL Server 2014 Dialect -->
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> -->
<!-- Your required Database Name -->
<prop key="hibernate.default_schema">eventrydb</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
EventryApp/src/main/webapp/WEB-INF/EventryApp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" ></property>
<property name="suffix" value=".jsp" ></property>
</bean>
</beans>
hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Eventry API</title>
</head>
<body>
<h1>Hello Eventry API</h1>
</body>
</html>
【问题讨论】:
-
检查您的应用程序是否正确部署。部署时tomcat日志有错误吗?
-
不,当我尝试访问该页面时,tomcat 日志没有任何错误。
-
我明白了,但是您确实部署了应用程序,对吗?
-
我如何检查它?
标签: spring spring-mvc tomcat8