Issue
**index.jsp**
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
**web.xml**
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>/</url-pattern>
</servlet-mapping>
</web-app>
**Display.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hi there
</body>
</html>
**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">
<context:component-scan base-package="com.juzar.controllers"></context:component-scan>
</beans>
**AddController.java**
package com.juzar.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AddController {
@RequestMapping("/add")
public String add() {
System.out.println("hi there");
return "Display.jsp";
}
}
Problem: I am not able to understand why is this error in the image link is coming. I tried a lot but not able to find the root cause of it. It seems to me that there is something wrong in my project structure is that so ?
**Console output:**
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.43
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jan 28 2021 20:25:45 UTC
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.43.0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Users\Juzar\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932\jre
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 14.0.2+12-46
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: D:\apache-tomcat-9.0.43
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=D:\apache-tomcat-9.0.43
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Users\Juzar\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Users/Juzar/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.2.v20210201-0955/jre/bin/server;C:/Users/Juzar/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.2.v20210201-0955/jre/bin;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Juzar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts;C:\Users\Juzar\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk1.8.0_201\bin;C:\Users\Juzar\AppData\Local\Programs\Microsoft VS Code\bin;C:\MinGW\bin;C:\Users\Juzar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts;C:\Users\Juzar\AppData\Local\atom\bin;;C:\WINDOWS\System32;;.]
Jun 09, 2021 4:18:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8084"]
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1411] milliseconds
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.43]
Jun 09, 2021 4:18:31 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 09, 2021 4:18:31 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 09, 2021 4:18:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'dispatcher'
Jun 09, 2021 4:18:31 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'dispatcher'
Jun 09, 2021 4:18:32 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 1240 ms
Jun 09, 2021 4:18:32 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8084"]
Jun 09, 2021 4:18:32 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3806] milliseconds
Jun 09, 2021 4:18:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoMVC/add
Jun 09, 2021 4:18:59 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoMVC/add
index.jsp page
error
project structure
I would be so happy if someone can find me the root cause. I am now just frustrated and without network help I wont be able to move further. Please help!!!
Solution
You need enable Annotation-Driven injection in the container. In your case, declare at dispatcher-servlet.xml
(I assumed you don't use JavaConfig).
<context:annotation-config/>
By the way, how about consider to use InternalResourceViewResolver
and place jsp file under WEB-INF
folder.
Because keeping jsp file under webapp
folder (directly) will result like user can access jsp directly.
For example, user can access hi there
without passing t1
and t2
by typing http://localhost:8080/DemoMVC/Display.jsp
url from browser. We don't want that, right?
InternalResourceViewResolver
configuration example:
<!-- 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>
Above configuration will be served as /WEB-INF/view/Display.jsp
with following code -
@RequestMapping("/add")
public String add() {
System.out.println("hi there");
return "Display";
}
Answered By - Byaku