Issue
I have been taking this Spring course on Udemy and the instructor uses Eclipse JEE since it is free. However I have recently gotten IntelliJ IDEA Ultimate Edition and naturally I would rather use that. I started a new Spring MVC project and I discovered that configuring Tomcat had an extra step of adding an Artifact (I feel this may be important to finding a solution) which Eclipse JEE doesn't seem to be concerned with. I have no idea what that is so I just went with the Web Application Exploded artifact. The URL used is http://localhost:8080/SpringMVCDemo_war_exploded/. When I run the Tomcat Server this URL displays index.jsp automatically. This is my problem. I want it to display a different jsp file. Namely main-menu.jsp found in web/WEB-INF/view/
. Let me explain a little more.
I have a HomeController
class annotated with @Controller
and a method that returns "main-menu" as a String
which through the dispatcher-servlet.xml
, Spring will prepend WEB-INF/view/
and append .jsp
. In theory this should display the main-menu.jsp page found in WEB-INF/view/
. However, index.jsp
is always displayed by default and I can't even get a link that navigates to main-menu.jsp
to work. I feel that there is something inconsistent with the URL used for the Exploded Artifact and the @RequestMapping("/")
annotated method in my HomeController
that returns "main-menu".
The program runs fine in Eclipse JEE. My hunch is that I need to do more configuring with The Tomcat Server and the Web Application Exploded artifact but I don't know anything about Artifacts and the IntelliJ documentation explains Artifacts like you already know what an Artifact is. Has anyone used Spring MVC in IntelliJ IDEA professional edition? How do I properly configure the server to respond to my @RequestMapping
? If possible I would like to leave the @RequestMapping
as is to make it easier to follow along in the course.
Below is the project structure and all files in question
<?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"
id="WebApp_ID" version="3.1">
<display-name>DisplayName</display-name>
<absolute-ordering/>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<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>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HomeController.java
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
Solution
Delete index.jsp
file from the project. It has priority over request mappings.
Don't have any files that would be considered default pages (index.html
, index.jsp
, default.html
, etc).
Answered By - CrazyCoder