Tomcat在http:// localhost:8080 /中运行,但是在尝试http:// localhost:8080 / page / home.jsp

时间:2018-10-04 12:51:10

标签: spring spring-mvc tomcat8

我已经用spring和hibernate创建了一个maven项目。我在这个项目中配置了tomcat 8.5。 第一次尝试运行tomcat服务器时,http://localhost:8080/不起作用。我在google中搜索,发现需要在tomcat中进行一些配置:

步骤:

  • 在Tomcat服务器上,单击鼠标右键>“属性”
  • 在“常规”菜单中:单击“切换位置”
  • 位置已从[工作区元数据]更改为localhost.server上的/ Server / Tomcat v8.5服务器

完成此配置后,我再次启动了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>

1 个答案:

答案 0 :(得分:0)

首先。对于您的本地雄猫所在的测试或开发目的,我都无所谓。我正在使用标准服务器位置(使用工作空间元数据)。但这与您的问题无关。

第二,您必须配置应用程序的基本路径。通常,这是在将应用程序添加到服务器时完成的(例如,使用Eclipse时)

如果这不是自动完成的,那么您必须在server.xml文件中写入以下内容

<Context docBase="myApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:myApp"/>

这使您的应用程序在“ http://localhost:8080/myApp”下可用

第三,您需要配置一个控制器来处理对该URL的请求。通常,您将需要类似登录公式的内容。

@Controller
@RequestMapping(value="/")
@Scope(value="session")
public class MainController {

  @RequestMapping
  public ModelAndView mainView() {
    ModelAndView mav = getModelAndView("mainView");
    return mav;
  }

}

我看到您有两个servlet定义。您是否真的想为您的应用程序提供多个上下文?您可能要结合这两个。