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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29204380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:30:17  来源:igfitidea点击:

Request resource is not found 404 error while running maven project on tomcat in eclipse

javaeclipsespringmaventomcat

提问by MAK

I am running my spring-rest application build on maven on tomcat in eclipse. While running the project on tomcat server it is showing 404 request resource is not found. I imported the rest project from( https://github.com/spring-projects/spring-data-book)enter image description here

我正在 Eclipse 中的 tomcat 上的 maven 上运行我的 spring-rest 应用程序。在 tomcat 服务器上运行项目时,它显示未找到 404 请求资源。我从(https://github.com/spring-projects/spring-data-book)导入了其余项目在此处输入图片说明

my web.xml file:

我的 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>

my ApplicationConfig.java file

我的 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;
}

}

}

I was struck here for an hour, So i am posting it here. can any one please help me. Thanks in advance.

我在这里被打了一个小时,所以我把它贴在这里。谁能帮帮我吗。提前致谢。

回答by Kasun Kariyawasam

Try http://localhost:8080/spring-data-book-reset/index.jspif not works then Manually copy the spring-data-book-reset.warfile into Tomcat/webapps/and up the server manually in this way --> Go to Tomcat/binin terminal and type sh catalina.sh run. Now try using your url. It might works. If it not works, somtimes it may be your configuration problem. Then post your web.xmland ApplicationContextConfiguration.xmlhere.

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

回答by Viswanath D

It's a common problem. Follow the steps:

这是一个常见的问题。按照步骤:

  • Click on Window > Show view > Server OR right click on the server in "Servers" view, select "Properties".

  • Open the Overview screen for the server by double clicking it. In the Server locations tab , select "Use Tomcat location".

  • Save the configurations and restart the Server.

  • 单击窗口 > 显示视图 > 服务器或在“服务器”视图中右键单击服务器,选择“属性”。

  • 通过双击打开服务器的概览屏幕。在服务器位置选项卡中,选择“使用 Tomcat 位置”。

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

That's it...!!

就是这样...!!

回答by Saurabh Jhunjhunwala

There could be multiple reasons for this issue.

此问题可能有多种原因。

  1. The war deployed does not correspond to the same name as requested
  2. As you are using spring, there might not be any confirguration xml used as servlet in the web.xml
  3. You might not have defined any welcome page.
  1. 部署的War与请求的名称不一致
  2. 当您使用 spring 时,web.xml 中可能没有用作 servlet 的任何配置 xml
  3. 您可能没有定义任何欢迎页面。

please share your web.xml and folder structure for better understanding

请分享您的 web.xml 和文件夹结构以便更好地理解

回答by Naveen Garg

Please check if your build path entries are being exported to Tomcat. Check the entries in Web Deployment Assembly under your project properties.

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

Hope this helps

希望这可以帮助