【问题标题】:Error 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists错误 404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
【发布时间】:2020-08-05 02:51:55
【问题描述】:

在服务器上运行我的 Web 应用程序时遇到 404 错误...我尝试了很多方法,但无法解决问题

我的控制器代码是..

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @RequestMapping("/list")
    public String listCustomers(Model theModel) {
        return "list-customers";
    }
}

我的 spring-mvc-crud-demo-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&amp;serverTimezone=UTC" />
        <property name="user" value="springstudent" />
        <property name="password" value="springstudent" /> 

        <!-- these are connection pool properties for C3P0 -->
        <property name="initialPoolSize" value="5"/>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>  
    
    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.luv2code.springdemo.entity" />
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
           </props>
        </property>
   </bean>    

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <!-- Add support for reading web resources: css, images, js, etc ... -->
    <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
</beans>

我的 web.xml 文件是..

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>spring-mvc-crud-demo</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc-crud-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

我使用的网址: http://localhost:8080/web-customer-tracker/customer/list

我的项目结构是...

请帮我解决这个问题..提前谢谢..

【问题讨论】:

  • (1) 除非有令人信服的理由,否则请使用 Spring Boot。 (2) 您的“所有客户”URL 通常应该只是 /customers(没有 ID 的 GET 表示“全部检索”)。 (3) 我认为问题是你没有映射/**,但是我已经7年没用过这种旧样式了,因为Boot让它变得不必要了。
  • 现在解决您的问题。
  • @AnishB。这是 github repo 的链接。这个 repo 将有一些额外的代码...我稍后插入...github.com/Venkat-juju/Spring-Project
  • 谢谢 :) 提供回购
  • @Anish B 你找到解决办法了吗?

标签: java spring spring-mvc


【解决方案1】:

首先,动态项目的创建方式不对。

我使用tomcat 8.5.57 versionJDK 8 进行测试。

我在 tomcat 上部署项目时遇到了这个问题:

More than one fragment with the name [spring_web] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering

我通过添加 &lt;absolute-ordering/&gt; 修复了 web.xml

该项目已部署在 tomcat 上,并且正在运行。

截图:

我的github代码:https://github.com/anish-fullstack/web-customer-tracker

【讨论】:

  • B 非常感谢您宝贵的时间。但不幸的是,它对我不起作用。我认为会有一些环境问题。你能在linkedin上与我联系吗?我已经向你发送了一个连接请求。谢谢。
  • B 非常感谢您的时间和支持.. :-)
猜你喜欢
  • 2020-02-27
  • 2023-03-26
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 2019-10-14
  • 1970-01-01
相关资源
最近更新 更多