I am trying to create a new spring mvc project which just displays "This is a sample spring MVC Demo Page". When running, I get a 404 error

Answered

I created a new Spring MVC project, added application server as Tomcat and gave it the path to find tomcat, added sdk as well

In the new project, Created a package under the src folder called "com.luv2code.springdemo". Created a controller class inside this package called HomeController.

Contents of the HomeController look like this:

package com.luv2code.springdemo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

@RequestMapping("/")
public String showPage(){
return "index";
}
}

Created a folder inside the web->WEB-INF called "view", inside view I moved the index.jsp and the contents of index.jsp look like this
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Spring Demo</title>
</head>
<body>
<h1>This is a sample spring MVC Demo Page</h1>
</body>
</html>

web.xml file inside the WEB-INF looks like this:

<?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>

I have another xml file called spring-mvc-demo-servlet.xml under the WEB-INF and its contents are:
<?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>


I have a dispatcherservlet.xml and an applicationcontext.xml that was created automatically when I created the project as a spring mvc project.

 

The tomcat Localhost logs show error as 

08-Aug-2017 10:37:34.263 INFO [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Marking servlet [dispatcher] as unavailable
08-Aug-2017 10:37:34.264 SEVERE [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [dispatcher] in web application [] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

 

Description for 404 error says: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

0
3 comments

Please file a ticket to support at https://intellij-support.jetbrains.com/hc/requests/new and attach the complete sample project to reproduce the problem.

0
Avatar
Permanently deleted user

@Jdarvin, Have you managed to find out the reason for the problem?

0
Avatar
Permanently deleted user

Ultimately this means that the web server cannot find the indicated object. This is because the jars are missing in the deployment artifact. 

The way that I fixed this was to add it to the artifact. 

  • Go to your Project Structure->artifact

There you should see your artifact. i.e. something like project:Web explode
In the right side panel, you should see Available items / lib

  • right-click on lib. 

In the context menu, you should see the option ('Put ... / Extract ...). These will place the jars in the artifact that is being created. 

  • Save and retry.
0

Please sign in to leave a comment.