Spring MVC is not detecting my controllers

cristid9 :

I have a Spring MVC project with the following files:

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
      <display-name>Archetype Created Web Application</display-name>
       <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appconfig-root.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></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>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

And 2 configuration files:

/src/main/webapp/WEB-INF/appconfig-root.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       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">

    <import resource="appconfig-mvc.xml"/>

    <context:component-scan base-package="yc.servlets"/>

    <!--<context:property-placeholder location="classpath:application.properties"/>-->
</beans>

/src/main/webapp/WEB-INF/appconfig-mvc.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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">

    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>

        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

And the class that contains my controller:

package yc.servlets;

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

@Controller
public class TestPage {


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

I think this is enough to get a working spring mvc, but when I type localhost:8888 in browser I get a 404 error.

localhost:8888/hello_there also gives 404 error.

Mykhailo Moskura :

Hi you are returning "hello" in your controller and its going to find hello.jsp If it dont find it will throw 404 http error

As you configured your InternalViewResolver :

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>

        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

It is trying to find in /WEB-INF/jsp/hello.jsp , so when you are returning String "hello "in controller :

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

It will add a suffix ".jsp" to "hello" and it will try to find "hello.jsp" as it could not find it it will throw 404

You can change from your @Controller to @RestController which adds implicitly @ResponseBody annotation and see if it returns "hello" in response

Also if this does not help you can change:

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

to this one :

<servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

If its still now working try next steps:

Try such url :

localhost:8080/yourApplicationName/hello_there

default conext-path its your app name here If you go to TOMCAT_HOME/webapps/

you will find that in root folder there is not your application

So thats why it could not found by localhost:8080/...

You can fix this by two ways:

1.Find your application deployed and copy to root folder in 
    TOMCAT_HOME/webapps/ROOT
 2.See the name of your application in  TOMCAT_HOME/webapps/ 
   and call url : localhost:8080/yourAppName/hello_world   

Thanks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97080&siteId=1