0

I am learning Spring MVC and i have big trouble with running my web application. My controllers do not work, so I can not map my request. Here is my error:

HTTP Status 404 – Not Found

Type Status Report

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

The same error occurs when i type in browser:

http://localhost:8081/showForm

http://localhost:8081/mvcapp/showForm

http://localhost:8081/processForm

My controller:

@Controller

    public class HelloWorldController {


        @RequestMapping("/showForm")
        public String showForm() {
            return "helloworld-form";
        }

        @RequestMapping("/processForm")
        public String processForm() {
            return "helloworld";
        }
    }

dispatcher-servlet

<?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/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">


    <mvc:annotation-driven/>
    <context:component-scan base-package="mvcapp"/>




    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <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/dispatcher-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>

File tree image: https://i.stack.imgur.com/QSNlg.png

Lukiniaz
  • 1
  • 1
  • Use Spring Boot, not legacy configuration. That will resolve this problem (which is probably that your pattern isn't `/**`). – chrylis -cautiouslyoptimistic- Dec 02 '19 at 23:49
  • I see you are using a non-standard port `8081`. Have you configured tomcat to listen to 8081? (default is 8080) – Maurice Dec 02 '19 at 23:57
  • I changed only http port from 8080 in intellij configuration (Edit configurations... >> Tomcat Server Settings) because I had error "Address localhost:8080 is already in use" – Lukiniaz Dec 03 '19 at 00:10
  • Does this answer your question? [Difference between / and /\* in servlet mapping url pattern](https://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern) – hc_dev Dec 03 '19 at 00:14

3 Answers3

0

Within your bean-configuration dispatcher-servlet.xml, try correcting the InternalResourceViewResolver bean's property named prefix to a relative path starting with /:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

or alternatively written:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>

    </bean>

See mkYong (2010): Spring MVC InternalResourceViewResolver example

hc_dev
  • 8,389
  • 1
  • 26
  • 38
0

when you visit http://localhost:8081/showForm, can it enter method, you can add System.out.println("showForm) before return "helloworld-form"; to location proplem, dispather proplem or jsp visit problem

TiantianHe
  • 48
  • 1
  • 6
0

I extended tomcat output adding WEB-INF/classes/logging.properties from content:

org.apache.catalina.core.ContainerBase.[Catalina].level=INFO

org.apache.catalina.core.ContainerBase.

[Catalina].handlers=java.util.logging.ConsoleHandler

And i found this exception:

03-Dec-2019 17:25:41.190 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Marking servlet [dispatcher] as unavailable 03-Dec-2019 17:25:41.191 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [dispatcher] in web application [] threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

(...)

So, problem was with missing dispatcher class and other libs.

I fixed this problem adding all spring libs from maven to libs directory. https://stackoverflow.com/a/24586000/12296902 Thanks a lot for reply

Community
  • 1
  • 1
Lukiniaz
  • 1
  • 1