Raspberry
Raspberry

Reputation: 337

Spring MVC home page

I have HomeController class:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage() {
        return "main-menu";
    }
}

My project structure: enter image description here

Spring version: 4.3.9 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">
    <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>
    <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>*.form</url-pattern>
    </servlet-mapping>
</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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

It always shows index.jsp, never main-menu.jsp I want a main-menu my Home Page. What should i do?

Upvotes: 0

Views: 2296

Answers (3)

Du-Lacoste
Du-Lacoste

Reputation: 12767


Briefly your web application will look into welcome page and /directMe is invoked which is captured by controller class and execute the logic you have implemented.

Add this in web.xml

 <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

In the JSP itself, add below coding:

<c:redirect url="/directMe"/>

Lastly, in the controller:

@RequestMapping(value = "/directMe", method = RequestMethod.GET)
        public String myMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException { 
// add your logic to execute
        return "main-menu"; // returns main-menu page with your logic
}

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148890

The problem lies in your web.xml file:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

You declare to the servlet container that SpringMVC DispatcherServlet will only process URL ending in .form. So the request for home never reaches SpringMVC machinery - BTW, that also explains why divine's proposal of /welcome does not work either, but /welcome.form should...

You could also have a look to this other post from mine for a general discussion of how to process the root URL with SpringMVC.

Upvotes: 1

divine
divine

Reputation: 4912

i've used an approach to solve this problem in my project. i'm sharing it with you below

Keep only below content in your index.jsp

index.jsp

<meta http-equiv="refresh" content="0;url=welcome" />

In your controller java program the RequestMapping must hold the value specified in the url attribute of <meta> tag specified in index.jsp file.

In this example, the url attribute has value welcome

@Controller
public class HomeController {

    @RequestMapping("/welcome")
    public String welcome() {
        return "main-menu";
    }
}

This worked for me

Updated #1:

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

Updated #2:

<context:annotation-config />
<context:component-scan base-package="add your base folder here" />

Upvotes: 1

Related Questions