在eclipse中运行tomcat上的maven项目时,找不到请求资源404错误

时间:2015-03-23 06:30:46

标签: java eclipse spring maven tomcat

我在eclipse上的tomcat上运行我的Spring-rest应用程序构建。在tomcat服务器上运行项目时,显示404请求资源未找到。我从(https://github.com/spring-projects/spring-data-bookenter image description here

导入了其余项目

我的web.xml文件:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<display-name>roo-spring-data-jpa</display-name>

<description>Roo generated roo-spring-data-jpa application</description>


<!-- Enable escaping of form submission contents -->
<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>HttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>HttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Handles Spring requests -->
<servlet>
    <servlet-name>roo-spring-data-jpa</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>roo-spring-data-jpa</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>10</session-timeout>
</session-config>

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/uncaughtException</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/resourceNotFound</location>
</error-page>

我的ApplicationConfig.java文件

  package com.oreilly.springdata.rest;

  import javax.persistence.EntityManagerFactory;
  import javax.sql.DataSource;

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import       org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
 import org.springframework.orm.jpa.JpaTransactionManager;
 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;

  /**
  * Spring JavaConfig configuration class to setup a Spring container   and    infrastructure components like a
* {@link DataSource}, a {@link EntityManagerFactory} and a {@lin PlatformTransactionManager}.
* 
* @author Oliver Gierke
*/
 @Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
 class ApplicationConfig {

/**
 * Bootstraps an in-memory HSQL database.
     * 
 * @return
 * @see http 
 *      ://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database
 *      -support
 */
@Bean
public DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
}

/**
 * Sets up a {@link LocalContainerEntityManagerFactoryBean} to use Hibernate. Activates picking up entities from the
 * project's base package.
 * 
 * @return
 */
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setDatabase(Database.HSQL);
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(getClass().getPackage().getName());
    factory.setDataSource(dataSource());

    return factory;
}

@Bean
public PlatformTransactionManager transactionManager() {

    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return txManager;
}

}

我被震惊了一个小时,所以我在这里发帖。谁能帮帮我吗。提前谢谢。

4 个答案:

答案 0 :(得分:4)

如果不起作用,请尝试http://localhost:8080/spring-data-book-reset/index.jsp 然后 以这种方式手动将spring-data-book-reset.war文件手动复制到Tomcat/webapps/并手动上传服务器 - &gt;转到终端中的Tomcat/bin,然后输入sh catalina.sh run。现在尝试使用您的网址。它可能会奏效。 如果它不起作用,有时可能是您的配置问题。然后在此处发布您的web.xmlApplicationContextConfiguration.xml

答案 1 :(得分:1)

这是一个常见问题。请按照以下步骤操作:

  • 点击Window&gt;显示视图&gt;服务器或右键单击服务器中 &#34;服务器&#34;查看,选择&#34;属性&#34;。

  • 双击打开服务器的“概述”屏幕。在里面 服务器位置选项卡,选择&#34;使用Tomcat位置&#34;。

  • 保存配置并重新启动服务器。

那是...... !!

答案 2 :(得分:1)

这个问题可能有多种原因。

  1. 部署的战争与请求的名称不对应
  2. 当您使用spring时,web.xml中可能没有任何确认xml用作servlet
  3. 您可能没有定义任何欢迎页面。
  4. 请分享您的web.xml和文件夹结构以便更好地理解

答案 3 :(得分:0)

请检查您的构建路径条目是否正在导出到Tomcat。检查项目属性下的Web部署程序集中的条目。

希望这有帮助