0

I want SpringMVC to call a method when my-site.com loads that adds some attributes to the model just like my other working methods do. I originally used this annotation:

    @RequestMapping(value = "/", method = RequestMethod.GET)

The problem is, Spring ignores the method for some reason, yet allows everything else to work. As a work-around I have to declare it like this:

    @RequestMapping(value = "/unnecessary-page", method = RequestMethod.GET)

and put a link to my-site.com/unnecessary-page on the main page. This is very strange. How would I get this to work the way I originally intended?

edit: this is the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <!--Dispatcher Config-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatcher-servlet.xml</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>


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

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>com.web.search.context.ContextFinalizer</listener-class>
    </listener>

</web-app>
John
  • 775
  • 3
  • 11
  • 25
  • What do you get when browsing to the home page? Page not found? Blank page? If you get the JSP but the action handler isn't called, it may be because Spring doesn't see it at all, depending on how the Dispatcher is set up in the web.xml file. Also, the `@RequestMapping` at the class level is meaningless, since you are overriding it on all the methods. – Andreas Aug 16 '15 at 23:18
  • The site is essentially a search page. It was working fine before the handleHomePageRequest() method was added, and also loaded fine after it was added. The new method is basically ignored. I don't know what to do to make spring see it. I'll post the web.xml file. – John Aug 16 '15 at 23:25
  • Hmmm... Are you sure `/search` is working? Your `` is not matching that path. It would if you changed the pattern to `/*`, but that would prevent JSP's from working, I believe. – Andreas Aug 16 '15 at 23:41
  • Yes, /search has been, and still is, working fine – John Aug 16 '15 at 23:42
  • If I change value = "/" to value = "/example" then it works when I navigate to that page. How would I get it to work with value = "/"? – John Aug 17 '15 at 01:25
  • Possible duplicate of http://stackoverflow.com/questions/13405868/in-a-servlet-mapping-in-spring-mvc-how-do-i-map-the-root-of-a-url-pattern-direct – Andreas Aug 17 '15 at 01:50
  • I just read through it. It doesn't solve this problem. – John Aug 17 '15 at 02:51

1 Answers1

0
@RequestMapping(value = "/*")

Try to remove this line. I think this is your problem, hope so.

index.jsp

<html>
<body>
<h2>${mgs}</h2>
</body>
</html>

HomepageController.java

@Controller
public class HomePageController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getHomePage(ModelMap map) {
        map.addAttribute("mgs", "Hello, Have a nice day");
        return "index";
    }
}

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:p="http://www.springframework.org/schema/p"
    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-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    ">

    <!-- Use @Component annotations for bean definitions -->
    <context:component-scan base-package="ltvnc.java.lichking.*" />

    <!-- Use @Controller annotations for MVC controller definitions -->
    <mvc:annotation-driven />

    <!-- View resolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

web.xml

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Archetype Created Web Application</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>

</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ltvnc.java.lichking</groupId>
    <artifactId>example</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>example Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>example</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

it will display "Hello, have a nice day" when you start your app with url http://localhost:7080/example/

  • Unfortunately commenting that out didn't have any effect. – John Aug 17 '15 at 13:18
  • Oh, you have tag in your web.xml, is it index.jsp?? if you want to load a Controller when you start your web app, in your index.jsp file just need only <%response.sendRedirect("your start page")%>. I do that every time. My teacher sad shouldn't use RequestMapping value like "/", it's not cleanly, may be. Hope so – Thanh le huu Aug 18 '15 at 04:05
  • I don't have a welcome-file tag. I edited the post to show the web.xml. Can you be more specific about what you think will get `@RequestMapping(value = "/", method = RequestMethod.GET)` to work? – John Aug 18 '15 at 14:33
  • i've try to do the same thing @RequestMapping(value = "/", method = RequestMethod.GET), it's work fine. I've edit my answer. – Thanh le huu Aug 19 '15 at 06:30