简体   繁体   中英

How to fix Http Status 404 Not found?

I've just started with Spring framework, currently learning the spring MVC the configuration is just too complicated xD , anyway everytime i try to run the application on the server it gives me this page instead of main-menu.jsp page.

I've tried to add the @RequestMapping("/") to the method in my controller calss and the method should return a String as u can see in the code below

项目树 结果 HompePage.java

@Controller
public class HomePage {

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

main-menu.jsp page

<!DOCTYPE html>
<html>
<body>
<h2>Spring MVC Demo - Home Page</h2>
</body>
</html>

spring-mvc-demo-servelet.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: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">

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

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

    <!-- Step 5: 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>

</beans>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <display-name>SpringMVC</display-name>  
   <servlet>    
    <servlet-name>spring</servlet-name>    
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    <load-on-startup>1</load-on-startup>      
</servlet>    
<servlet-mapping>    
    <servlet-name>spring</servlet-name>    
    <url-pattern>/</url-pattern>    
</servlet-mapping>    
</web-app>

I've got Http status 404 - Not found

Logs

Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.24
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Aug 14 2019 21:16:42 UTC
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.24.0
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            4.18.0-25-generic
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /usr/lib/jvm/java-12-oracle
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           12.0.1+12
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/ayoub/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /home/ayoub/Desktop/apache-tomcat-9.0.24
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/ayoub/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/ayoub/Desktop/apache-tomcat-9.0.24
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/home/ayoub/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Sep 05, 2019 3:37:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Sep 05, 2019 3:37:32 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib]
Sep 05, 2019 3:37:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 05, 2019 3:37:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 05, 2019 3:37:34 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [3,226] milliseconds
Sep 05, 2019 3:37:34 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Sep 05, 2019 3:37:34 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.24]
Sep 05, 2019 3:37:35 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 05, 2019 3:37:35 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 05, 2019 3:37:35 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [1,524] milliseconds

your web.xml should be like this you have to give servelt name

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <display-name>SpringMVC</display-name>  
   <servlet>    
    <servlet-name>spring</servlet-name>    
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    <load-on-startup>1</load-on-startup>      
</servlet>    
<servlet-mapping>    
    <servlet-name>spring</servlet-name>    
    <url-pattern>/</url-pattern>    
</servlet-mapping>    
</web-app>



@Controller
public class HomePage {
    @RequestMapping("/main-menu")
    public String showPage() {
        return "main-menu";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM