0

I'm trying to learn the Spring Mvc through writing some simple controller. This is what I've got so far:

HomeController.java

package com.mehran.controller;

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

@Controller
public class HomeController
{
    @RequestMapping(value = "/")
    public ModelAndView index()
    {
        ModelAndView mav = new ModelAndView("home");
        String msg = "Running HomeController.index() method";
        mav.addObject("msg", msg);
        return mav;
    }
}

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_3_1.xsd"
         version="3.1">

    <display-name>Intellij Idea Native Spring MVC</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

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

dispatcher-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: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">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.mehran.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

The given code above works fine and I can see the string in the output. But if I change the url-pattern to <url-pattern>/services</url-pattern> I can no longer get the output.

Am I misunderstanding something here? Shouldn't changing the url-pattern be enough to change all the controllers URLs from http://localhost:8080/MyApp to http://localhost:8080/MyApp/services?

[UPDATE]

I managed to find some log out of the catalina. I edited the logging.properties file and added org.apache.catalina.level=ALL. I'm not sure if there's anything more I can do or not! But here it goes:

09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/services]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/services
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint
09-Mar-2016 17:52:38.699 WARNING [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/MyApp/services] in DispatcherServlet with name 'dispatcher'
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/favicon.ico]
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]

[UPDATE]

As suggested I used <url-pattern>/MyApp/services</url-pattern> and here's the result:

09-Mar-2016 18:16:59.085 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/services/]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/services/
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint
09-Mar-2016 18:16:59.099 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/favicon.ico]
09-Mar-2016 18:16:59.100 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:16:59.100 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]

The warning's gone but the output result (in the browser) is still 404!

[UPDATE]

This time I added "/home" to the @RequestMapping and tried to navigate to /MyApp/services/home:

09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/serviecs/home]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/serviecs/home
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint
Mehran
  • 15,593
  • 27
  • 122
  • 221

2 Answers2

0

Try this pattern <url-pattern>/services/*</url-pattern> this works for me in my app

Filippo Fratoni
  • 379
  • 2
  • 7
  • Thanks but it doesn't work for me! I've tested all you can imagine and it doesn't work. The worth part is that I can not find the error log. It's a docker image and the standard out put shows no error, what so ever! – Mehran Mar 09 '16 at 17:17
  • 1
    @Mehran Then your logging may be misconfigured (assuming there's an error). – Dave Newton Mar 09 '16 at 17:33
  • @DaveNewton There's no `/usr/local/tomcat/logs/catalina.out` and when I `attach` to the container, there's no entry added to the stdout when I refresh the browser! Can you please help me find the error log? – Mehran Mar 09 '16 at 17:37
0

Seems you have deployed your application as Root context as the MyApp is not getting treated as webcontext. It is looking for a end point "/MyApp/services/"

Try changing the url pattern to "/MyApp/services" to confirm.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71