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

我正在尝试在 intellij 上使用 spring 配置项目,但仍然显示错误 404。配置文件来自 udemy 课程任何建议都会有所帮助。

Tomcat 版本:9.0.7

项目结构: Project structure

控制器类:

package com.Controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/")
public String showPage(){
    return "main";
}
}

ma​​in.jsp:

<!DOCTYPE html>
<html>
<body>
<h2>Works</h2>
</body>
</html>

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>test2</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>
        <!--suppress XmlPathReference -->
        <param-value>\WEB-INF\spring-mvc-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>

spring-mvc-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.Controllers" />
<context:component-scan base-package="com" />
<!-- 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/webservice?useSSL=false" />
    <property name="user" value="root" />
    <property name="password" value="root" />

    <!-- these are connection pool properties for C3P0 -->
    <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.Entity" />
    <property name="hibernateProperties">
       <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>


spring-mvc-demo-servlet.xml:

<!-- 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="/web/resources/" mapping="/resources/**"></mvc:resources>

【问题讨论】:

  • 您可以发布您遇到的错误的屏幕截图吗?另外,如果您可以共享日志,那就太好了。
  • 肯定有截图:screen
  • catalina 日志:pastebin.com/YcxriXn3
  • tomcat9-stderr 日志:pastebin.com/8k1dMdaH
  • 你正在调用 /test2,而在你的控制器中你正在监听 "/",尝试添加 "/test2 并检查它。@RequestMapping("/test2")

标签: java spring tomcat


【解决方案1】:

尝试像这样在@Controller 下添加@RequestMapping("/"):

@Controller
@RequestMapping("/")
public class HelloController {

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public String showPage(){
 return "main";
 }
}

【讨论】:

    【解决方案2】:

    我遇到了和你类似的问题。这是因为项目结构。

    请检查您的main.jsp 层次结构。

    必须在下面

    ../WEB-INF/view/main.jsp

    你必须像这样称呼它

    http://localhost:portNumber/yourProjectName/

    (例如:http://localhost:8080/1-SpringMVC-Demo/

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 2019-06-13
      相关资源
      最近更新 更多