0

I'm in trouble and would like your help. I'm a beginner in Spring MVC (and Spring at all). I have followed the http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/ but it isn't working on. I added a welcome file (index.jsp). When i enter (http://localhost:8080/SpringMVC) all right. But when i add the controller pattern (http://localhost:8080/SpringMVC/welcome), it doesn't work (HTTP Status 404). Here my configs:

web.xml

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Spring Web MVC Application</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.mkyong.common.controller" />
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

and my folder structure is:

-> src
   -> main
      -> java
         -> com
            -> mkyong
               -> common
                  -> controller
                     -> HelloController.java
      -> resources
      -> webapp
         -> index.jsp
         -> WEB-INF
            -> mvc-dispatcher-servlet.xml
            -> web.xml
            -> pages
               -> hello.jsp

Someone can help me?

4
  • Can I recommend turning the log level to DEBUG and seeing what Spring prints out - if Spring DispatcherServlet gets the request then you will see a lot of information in the logs that should clearly indicate what is going wrong. Commented Aug 6, 2012 at 19:01
  • Try placing this content in a log4j.properties file in the classpath - log4j.rootLogger=trace, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n Commented Aug 6, 2012 at 19:17
  • When i enter localhost:8080/SpringMVC he says: WARN: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'mvc-dispatcher'. Commented Aug 6, 2012 at 19:27
  • Very good, now it is little more clear - So your context is deployed at root context (/) not /SpringMVC, try the first answer and see what message you get localhost:8080/welcome Commented Aug 6, 2012 at 19:29

4 Answers 4

1

404 means that the requested resource cannot be found. Make sure your controller is annotated with:

@Controller and @RequestMapping("/welcome") 

From the link:

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

The class HelloController is marked with @Controller, RequestMapping as it must be.
0

You have your servlet mapped to '/', and the controller you want to hit is accepting requests for '/welcome'

The request you need to make should be (http://localhost:8080/welcome). I don't know why he has that extra 'SpringMVC' in his example.

Also, add <mvc:annotation-driven/> to your mvc-dispatcher-servlet.xml to make sure it's using annotations on your controller(s). And add the mvc XML namespace also. My namespaces look like this:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

I would suggest getting rid of that welcome file until you have your controller working correctly to keep things simple

6 Comments

Making such request localhost:8080/welcome still returns HTTP Status 404. I don't know what is happening.
What version of spring are you using?
3.0.5.RELEASE according to the pom.xml.
@mnatan.brito edited answer. try adding mvc annotation stuff
Are you talking about @Controller, @RequestMapping?
|
0

You must have a Controller mapped to "/".

@Controller
@RequestMapping("/")
public class StartController {

  @RequestMapping(method = RequestMethod.GET)
  public String printWelcome(ModelMap model) {

    model.addAttribute("message", "Spring 3 MVC Hello World");
    return "hello";

  }

}

Comments

0

Include this library on your view to enable ${}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

This will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.