0

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?

Raspberry
  • 329
  • 1
  • 4
  • 16

2 Answers2

1

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" />
divine
  • 4,353
  • 2
  • 27
  • 34
  • Doesn't work for me. IDE always says "No view resolvers found" – Raspberry Jul 06 '17 at 13:03
  • 1
    do you have viewResolver defined in yourdispatcher-servlet.xml? – divine Jul 06 '17 at 13:05
  • No, look at dispatcher-servlet.xml in post – Raspberry Jul 06 '17 at 13:08
  • @Raspberry i have updated the code for view resolver in the answer.verify it – divine Jul 06 '17 at 13:11
  • @Raspberry place the main-menu.jsp inside the pages folder in case if you are following the same code i have posted – divine Jul 06 '17 at 13:13
  • ok, i added that bean to dispatcher-servlet.xml, IDE now found main-menu.jsp, but when i run project i have 404 error on http://localhost:8081/welcome – Raspberry Jul 06 '17 at 13:32
  • @Raspberry you need to add some entries in dispatcher-servlet.xml for scanning the annotations for autoconfig. i think you didnot add those and that is causing the trouble. Check my updated #2 answer – divine Jul 06 '17 at 13:38
1

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.

Serge Ballesta
  • 136,215
  • 10
  • 111
  • 230