Issue
[see updates at the bottom] I'm trying to launch a Spring web app via Tomcat, but when I enter the url http://localhost:8080/demo-mvc/ in the browser, I get the 404 error and a message "The requested resource [/demo-mvc/] is not available". At first I was trying to deploy the app via NetBeans and it said that deployment was successful, but at browser launch I got the 404 error. I checked the Tomcat/webapps directory and didn't find the app directory there somewhy, so I decided to manually pick the app directory from NetBeans' projects folder and move it to webapps (and rename it to demo-mvc for brevity), still there's an error. The content of app's config files is the following:
web.xml
<?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>spring-mvc-demo</display-name>
<!-- 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/spring-mvc-demo-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>
spring-mvc-demo-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>
There's also a source file 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";
}
}
and a main-menu.JSP file in the WEB-INF\view location.
UPD: I tried moving the war to webapps, the corresponding folder appeared automatically. Now the requested resource message is gone, but the 404 is still there.
UPD: There are actually two controllers in the app and when I add /hello/showForm to the url, the appropriate page shows up, so there's something wrong with the main page only. The second controller code is: HelloWorldController.java
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/showForm")
public String showForm()
{
return "helloworld-form";
}
@RequestMapping("/processForm")
public String processForm()
{
return "helloworld";
}
...
}
And there is helloworld-form.jsp in the view folder.
Solution
The issue is resolved. There was a default index.html file in the webapp directory created by NetBeans. When I deleted that file, the request mapping worked and my index page showed up.
Answered By - Summit2
Answer Checked By - Terry (JavaFixing Volunteer)