Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC Hello World application not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC Hello World application not working

    Hi, I'm new to Spring MVC and I'm trying to write a simple web app to get started with Spring.

    Here's what I have:

    web.xml:
    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>HatifimWeb</display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>main</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>main</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    main-servlet.xml
    PHP Code:
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        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="springapp.controller" />
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsps/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    LoginController.java
    PHP Code:
    package springapp.controller;

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

    @
    Controller
    public class LoginController {

        @
    RequestMapping("/Login")
        public 
    ModelAndView handleRequest() throws Exception {
            
    System.out.println("Login...");

            return new 
    ModelAndView("Login""message""test");
        }


    index.jsp
    PHP Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        
    pageEncoding="ISO-8859-1"%>
    <!
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <
    title>Insert title here</title>
    </
    head>
    <
    body>
        
    Hello.
        <
    br />
        <
    a href="Login">login</a>
    </
    body>
    </
    html
    And finally Login.jsp
    PHP Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        
    pageEncoding="ISO-8859-1"%>
    <!
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <
    title>poopidipoo</title>
    </
    head>
    <
    body>
        ${
    message}
    </
    body>
    </
    html
    The index.jsp starts well, but when I click to navigate to the login page, I get 404.
    I'm using Tomcat 7. And I'm out of ideas, it looks like the DispatcherServlet is not being invoked for some reason.

    What could be the problem?

    Thanks in advance.

  • #2
    Your URL is relative where your controller is mapped to /Login so first fix your href...

    To get a feeling for what is going on I suggest you enable DEBUG logging for org.springframework.[web] and you can then see the mappings and request processing happening (ifyou really want to see stuff you might enable TRACE but this will output massive logging!).

    Comment


    • #3
      Hi, thanks for the answer.
      I added logging, and now I see that the view resolver is not adding the prefix and suffix
      Code:
      DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'main' processing GET request for [/HatifimWeb/Login.do]
      DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /Login.do
      DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView springapp.controller.LoginController.handleRequest() throws java.lang.Exception]
      DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/HatifimWeb/Login.do] is: -1
      Login...
      DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'Login'; URL [Login]] in DispatcherServlet with name 'main'
      DEBUG: org.springframework.web.servlet.view.InternalResourceView - Added model object 'message' of type [java.lang.String] to request in view with name 'Login'
      DEBUG: org.springframework.web.servlet.view.InternalResourceView - Forwarding to resource [Login] in InternalResourceView 'Login'
      DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
      It's supposed to change the Login to WEB-INF/Login.jsp but it does not do that. What could be the problem?
      Last edited by Bennyz; Aug 12th, 2012, 01:04 PM. Reason: Code tags

      Comment


      • #4
        Please use [ code][/code ] tags when posting log messages...

        Also judging by the debug messages the code you posted isn't the code used to generate the messages (login.do isn't mapped in the controller you posted). Also the configuration you posted isn't complete and you are using spring 3.1 and not 3.0 (as the configuration suggests). So can you post your actual configuration (including @Configuration classes).

        Also your viewresolver should have the order property set to 1 (especially if you use @EnableWebMvc to override the additional registered view resolvers). I also suggest you take a look at the startup log to see which beans (especially view resolvers) are registered.

        Comment


        • #5
          Sorry, I edited the previous reply.

          I changed the web.xml so the mapping of the main servlet will be to *.do instead if /, since I had a problem with it.I also changed the index.jsp href to Login.do.

          I have another configuration file servilet-context.xml:
          Code:
          <?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:mvc="http://www.springframework.org/schema/mvc"
              xmlns:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="
                  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                  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">
           
              <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
           
              <!-- Scans within the base package of the application for @Components to configure as beans -->
              <!-- @Controller, @Service, @Configuration, etc. -->
              <context:component-scan base-package="springapp" />
           
              <!-- Enables the Spring MVC @Controller programming model -->
              <mvc:annotation-driven />
           
          </beans>
          I thought I could move its settings to -servlet.xml, but it throws an exception on deployment. I think it might have to do something with my problem, but I have no idea really.

          And this is the updated web.xml:
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
              id="WebApp_ID" version="2.5">
              <display-name>HatifimWeb</display-name>
              <welcome-file-list>
                  <welcome-file>index.jsp</welcome-file>
              </welcome-file-list>
              <servlet>
                  <servlet-name>main</servlet-name>
                  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          
                  <init-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>/WEB-INF/spring_config/servlet-context.xml</param-value>
                  </init-param>
                  <load-on-startup>1</load-on-startup>
              </servlet>
              <servlet-mapping>
                  <servlet-name>main</servlet-name>
                  <url-pattern>*.do</url-pattern>
              </servlet-mapping>
          	<context-param>
          	    <param-name>log4jConfigLocation</param-name>
          	    <param-value>/WEB-INF/log4j.xml</param-value>
          	</context-param>
          	<listener>
              	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
          	</listener>
          </web-app>

          Comment


          • #6
            Currently there is no viewresolver so not sure what you would expect to happen?!

            Comment


            • #7
              The view resolver is in the main-servlet.xml, in my first post

              Comment


              • #8
                Which is useless that configuration isn't loaded as you specify which files to load... Hence your main-servlet.xml isn't being loaded... Also why have 2 files instead of putting it in 1?!

                Comment

                Working...
                X