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
  • 337
  • 2
  • 4
  • 16

3 Answers3

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,746
  • 3
  • 27
  • 38
  • 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
  • 143,923
  • 11
  • 122
  • 252
0

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
}
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51