错误 404:源服务器未找到目标资源的当前表示,或者不愿透露其存在。

huangapple go评论84阅读模式
英文:

Error 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

问题

我在服务器上运行我的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">

    <!-- 添加组件扫描支持 -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- 添加转换、格式化和验证支持 -->
    <mvc:annotation-driven/>

    <!-- 定义Spring MVC视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 步骤1:定义数据库数据源/连接池 -->
    <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" /> 

        <!-- 这些是C3P0的连接池属性 -->
        <property name="initialPoolSize" value="5"/>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>  

    <!-- 步骤2:设置Hibernate会话工厂 -->
    <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>  

    <!-- 步骤3:设置Hibernate事务管理器 -->
    <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 步骤4:基于注解启用事务行为的配置 -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <!-- 添加对Web资源的支持:css、图像、js等... -->
    <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>

我使用以下URL:
http://localhost:8080/web-customer-tracker/customer/list

我的项目结构如下图所示:(请参考链接中的图片)

希望这能帮助您解决问题。事先感谢!

英文:

I'm getting 404 error while running my web application on server... I tried many things but i'm unable to resolve the issue

My controller code is..

@Controller
@RequestMapping(&quot;/customer&quot;)
public class CustomerController {
@RequestMapping(&quot;/list&quot;)
public String listCustomers(Model theModel) {
return &quot;list-customers&quot;;
}
}

my spring-mvc-crud-demo-servlet.xml file is as below....

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
xsi:schemaLocation=&quot;
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">
&lt;!-- Add support for component scanning --&gt;
&lt;context:component-scan base-package=&quot;com.luv2code.springdemo&quot; /&gt;
&lt;!-- Add support for conversion, formatting and validation support --&gt;
&lt;mvc:annotation-driven/&gt;
&lt;!-- Define Spring MVC view resolver --&gt;
&lt;bean
class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot; /&gt;
&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
&lt;/bean&gt;
&lt;!-- Step 1: Define Database DataSource / connection pool --&gt;
&lt;bean id=&quot;myDataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;
destroy-method=&quot;close&quot;&gt;
&lt;property name=&quot;driverClass&quot; value=&quot;com.mysql.cj.jdbc.Driver&quot; /&gt;
&lt;property name=&quot;jdbcUrl&quot; value=&quot;jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&amp;amp;serverTimezone=UTC&quot; /&gt;
&lt;property name=&quot;user&quot; value=&quot;springstudent&quot; /&gt;
&lt;property name=&quot;password&quot; value=&quot;springstudent&quot; /&gt; 
&lt;!-- these are connection pool properties for C3P0 --&gt;
&lt;property name=&quot;initialPoolSize&quot; value=&quot;5&quot;/&gt;
&lt;property name=&quot;minPoolSize&quot; value=&quot;5&quot; /&gt;
&lt;property name=&quot;maxPoolSize&quot; value=&quot;20&quot; /&gt;
&lt;property name=&quot;maxIdleTime&quot; value=&quot;30000&quot; /&gt;
&lt;/bean&gt;  
&lt;!-- Step 2: Setup Hibernate session factory --&gt;
&lt;bean id=&quot;sessionFactory&quot;
class=&quot;org.springframework.orm.hibernate5.LocalSessionFactoryBean&quot;&gt;
&lt;property name=&quot;dataSource&quot; ref=&quot;myDataSource&quot; /&gt;
&lt;property name=&quot;packagesToScan&quot; value=&quot;com.luv2code.springdemo.entity&quot; /&gt;
&lt;property name=&quot;hibernateProperties&quot;&gt;
&lt;props&gt;
&lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
&lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/prop&gt;
&lt;/props&gt;
&lt;/property&gt;
&lt;/bean&gt;	  
&lt;!-- Step 3: Setup Hibernate transaction manager --&gt;
&lt;bean id=&quot;myTransactionManager&quot;
class=&quot;org.springframework.orm.hibernate5.HibernateTransactionManager&quot;&gt;
&lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
&lt;/bean&gt;
&lt;!-- Step 4: Enable configuration of transactional behavior based on annotations --&gt;
&lt;tx:annotation-driven transaction-manager=&quot;myTransactionManager&quot; /&gt;
&lt;!-- Add support for reading web resources: css, images, js, etc ... --&gt;
&lt;mvc:resources location=&quot;/resources/&quot; mapping=&quot;/resources/**&quot;&gt;&lt;/mvc:resources&gt;
&lt;/beans&gt;

my web.xml file is..

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;3.1&quot;&gt;
&lt;display-name&gt;spring-mvc-crud-demo&lt;/display-name&gt;
&lt;welcome-file-list&gt;
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/spring-mvc-crud-demo-servlet.xml&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
&lt;url-pattern&gt;/&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;/web-app&gt;

I used the url:
http://localhost:8080/web-customer-tracker/customer/list

My project structure is...
错误 404:源服务器未找到目标资源的当前表示,或者不愿透露其存在。

Kindly help me with this.. Thanks in advance..

答案1

得分: 1

首先,动态项目创建方式不正确。

我在测试中使用了 tomcat 8.5.57 版本JDK 8

在将项目部署到 tomcat 时,我遇到了以下问题:

发现了名称为 [spring_web] 的多个片段。这在相对排序中是不合法的。有关详细信息,请参阅Servlet规范的第8.2.2 2c节。考虑使用绝对排序。

我通过在 web.xml 中添加 &lt;absolute-ordering/&gt; 来解决了这个问题。

项目已部署到 tomcat 并且正常工作。

截图:

错误 404:源服务器未找到目标资源的当前表示,或者不愿透露其存在。

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

英文:

First of all, the dynamic project is created in a wrong way.

I have used tomcat 8.5.57 version for my testing with JDK 8.

I was facing this issue while deploying the project on 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

I fixed the web.xml with adding &lt;absolute-ordering/&gt;.

The project is deployed on tomcat and it's working.

Screenshot :

错误 404:源服务器未找到目标资源的当前表示,或者不愿透露其存在。

My github code : https://github.com/anish-fullstack/web-customer-tracker

huangapple
  • 本文由 发表于 2020年8月5日 10:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63257733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定