Invalid controller configuration at Spring MVC 4

569 views Asked by At

I am new to Spring framework. I would to develop a simple web-app that displays a hello.jsp content based on url "/greeting.html". But now, it gives me 404 error. What am I doing wrong?

Here is the github repo for my project (this project was created under Eclipse STS): https://github.com/terancet/EventTracker

Here is my HelloController class

@Controller
public class HelloController {
    @RequestMapping(value = "/greeting")
    public String sayHello(Model model) {
        model.addAttribute("greeting", "Hello World");

        return "hello.jsp";
    }

}

Here is web.xml

<servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>


        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.pluralsight.WebConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

Here is a project structure:

project structure

3

There are 3 answers

1
Selwyn On BEST ANSWER

add a log4j.xml or log4j.properties into the

src/main/resources

folder (it will automatically be detected). Then when you start up the server it will tell you exactly where the url mapping is for your controller via info log. This is important not to only verify the mapping url but to make sure it is actually being mapped:

Also, in STS/Eclipse if you:

right click on the project -> click properties -> click select 'Web Project Settings' 

enter image description here

by default that is where the url path is derived. So if the context-root value in there is: 'EventTracker' then your full url request will be (assuming a '.jsp' suffix is set as others have indicated in your resolver):

http://localhost/EventTracker/greetings

Sample log4j.xml from 1 of my projects:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 

    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%-5p:%d{yyyyMMdd HH:mm:ss} %t %c - %m%n"/>
        </layout>
    </appender>

    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
       <param name="File" value="c:/tmp/my-app.log"/>
       <param name="MaxFileSize" value="10MB"/>
       <param name="MaxBackupIndex" value="50"/>

       <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p:%d{yyyyMMdd HH:mm:ss} %t %c - %m%n" />
        </layout>
    </appender>

    <!-- 3rdparty Loggers -->
    <logger name="org.springframework.core">
        <level value="info" />
    </logger>

    <logger name="org.springframework.beans">
        <level value="info" />
    </logger>

    <logger name="org.springframework.context">
        <level value="info" />
    </logger>

    <logger name="org.springframework.web">
        <level value="debug" />
    </logger>

    <logger name="org.springframework.webflow">
        <level value="debug" />
    </logger> 


    <!-- Root Logger -->
    <root>
        <priority value="info" />
        <appender-ref ref="console" />
        <appender-ref ref="fileAppender"/>
    </root>


</log4j:configuration> 

log info:

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/userManagement/getUser],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.assessme.com.entity.User org.assessme.com.controller.UserManagementController.data(java.util.Locale,org.springframework.ui.Model)
2
M. Deinum On

In your WebConfig you have the following

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();

    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");

    return resolver;
}

Your controller is returning hello.jp. This is passed on the to InternalResourceViewResolver to be resolved to a view. It will create an path of /WEB-INF/jsp/hello.jsp.jsp and next it forwards to this path.

First make your controller return hello instead of hello.jsp. Second create a jsp directory inside the WEB-INF directory and move your hello.jsp there. Third restart.

2
Kyrylo Semenko On

It could be typo in application name. Please try localhost:8080/EventTracker/greeting - EventTracker with Event

And try to change url-pattern to /*