Why spring warn message is" org.springframework.web.servlet.PageNotFound "? - java

I trying spring 3 mvc this package is
org.spring.test and code is
#Controller
#RequestMapping("/welcome")
public class WelcomeController {
private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);
#RequestMapping(method = RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
}
#RequestMapping("test1")
public void test1() {
logger.info("test1!");
}
#RequestMapping("test2")
public void test2() {
logger.info("test2!");
}
}
web.xml is:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
last mvc.xml is
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.spring.test" />
<!-- Configures support for #Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
When I use "mvn jetty:run" command and write to http://localhost:8080/springdemo/welcome
is
"HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
NOT_FOUND"
Console message is :
INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
Why?

It looks like you are missing /WEB-INF/views/test.jsp. Is that true?
You are requesting /test URI, which is not mapped to your controller class (which is mapped to /welcome, I think). Spring uses InternalResourceViewResolver to serve your request. It takes prefix + "test" + suffix. That is how you end up with /WEB-INF/views/test.jsp.
Make sure you have that JSP or change your controller mappings.
Update:
Ok, you changed the question. But do you have /springdemo/WEB-INF/views/welcome.jsp ?

Related

Spring MVC : How url mapping was done before use of #RequestMapping() in spring framework?

I am learning spring MVC nowadays for new project.There I came across #RequestMapping annotations for how requests are mapped with URL.
I want to understand that , How request and URL is mapped previously without use of this annotation?
How it is configured in XML files?
Controller code:
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello(Model model){
model.addAttribute("greeting","Helloworld");
return "hello";
}
}
servlet-config.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:osgi="http://www.springframework.org/schema/osgi" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="/greeting.html" class="com.pj.controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Check this out: Difference between the annotations #GetMapping and #RequestMapping(method = RequestMethod.GET)
Nowadays,
RequestMapping is used to map the endpoint of your controller.
For the http verbs, there are specific mapping annotations like
#Getmapping,
#PostMapping,
#PutMapping...

Spring - RestController annotation does not catch request

I don't understand why If I use RestController annotation to declare a class as service, like this:
#RestController("/registration")
public class RegistrationService {
#RequestMapping(value="/",
produces="application/json")
public String initializeSession(Model model){
return "{\"success\":1}";
}
}
when I do a request like
http://localhost:8080/SpringRest/registration/
I get an 404 status and in the console:
No mapping found for HTTP request with URI [/SpringRest/registration/] in DispatcherServlet with name 'dispatcherServlet'
Everything works If I change #RestController("/registration")
with
#Controller
#RequestMapping("/registration")
and add #ResponseBody above method declaration.
This is my configuration:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SpringRest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<mvc:annotation-driven />
<!-- Uncomment and your base-package here: -->
<context:component-scan
base-package="it.reply.rest"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
The declaration of RestController looks like:
#Target(value=TYPE)
#Retention(value=RUNTIME)
#Documented
#Controller
#ResponseBody
public #interface RestController
It includes #Controller and #ResponseBody but not #RequestMapping. So you need to add it. So the following shall work:
#RestController
#RequestMapping("/registration")
The correct usage is:
#RestController
#RequestMapping("/registration")
The value of #RestController is the component name.
From RestController (Spring Framework 4.3.9.RELEASE API):
public abstract String value
The value may indicate a suggestion for a logical component name, to
be turned into a Spring bean in case of an autodetected component.
#RestController not including url mapping so you need to #Requestmapping as well like
#RestController
#RequestMapping("/registration")

Spring MVC controllers not loaded

I have a problem with Spring not picking up the controller. I've used maven webapp archetype to create a project in Eclipse and it worked fine - I could access the index.jsp page. Problems started when I added Spring to the project. (context, controller, etc.)
I'm using Eclipse Mars, Tomcat 8, maven 3.3.3 and Spring 4.2.2
Here is what I see in the log (probably only third line matters):
Looking for matching resources in directory tree [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller]
Searching directory [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller] for files matching pattern [C:/Users/dima/spring_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cte/WEB-INF/classes/com/company/dept/demo/cte/controller/**/*.class]
Resolved location pattern [classpath*:com/company/dept/demo/cte/controller/**/*.class] to resources []
Instead of something like
INFO: Mapped URL path ...
As a result I get 404 when trying to open the web page. These are the files:
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<context:component-scan base-package="com.company.dept.demo.cte.controller" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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>
And here is my controller:
package com.company.dept.demo.cte.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/pid")
public class PidController {
#RequestMapping(value="/hello")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView("pidHello");
mav.addObject("message", "hello world");
return mav;
}
}
When trying to access localhost:8080/cte/pid/hello 404 returned and the log says:
DispatcherServlet with name 'dispatcher' processing GET request for [/cte/pid/hello]
Looking up handler method for path /pid/hello
Did not find handler method for [/pid/hello]
No mapping found for HTTP request with URI [/cte/pid/hello] in DispatcherServlet with name 'dispatcher'
Thanks.
Update:
WAR file is correct but for some reason when it is deployed to Tomcat controller class isn't copied.
Is it your full web.xml , Because you have to add your dispatcher servlet reference to web.xml , Like below -
<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>

Running Java Servlet cant view welcome page 404 error

Having an issue like this.
Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found
web.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionAdet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionSure</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispacher-servlet.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\mvc-dispacher-servlet.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
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">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
hello.jsp
Here is my ..\MTSM\src\main\webapp\WEB-INF\pages\hello.jsp
$.get('ActionAdet', {opername: 'services'}, function (responseJson) { } ) ;..
hello.HelloController.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\HelloController.java
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "ss";
}
}
ActionAdet.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\ActionAdet.java
#Controller
#RequestMapping("/ActionAdet")
public class ActionAdet extends HttpServlet { } etc..
I can't view my project in this url: localhost:7001/MTSM/
and also not available in: localhost:7001/MTSM/hello.jsp
It was working 2 days ago. I think I'm deleted something in web.xml, but I don't remember.
Do you know what problem is?
I uploaded my project here: [Dropbox -- MTSM_20140112.zip][3]
Thanks.
According to the logs you provided in pastie (that's bad : you should have providen the relevant part as an edit), spring tried to load WEB-INF/pages/ss.jsp and did not find it. I means :
you probably hit the controller HelloController
the method printWelcome returned ss as a string
due to mvc-dispacher-servlet.xml, InternalResourceViewResolver looked for WEB-INF/pages/ss.jsp (normal untill here)
as the file does not exist you got a 404 error (still normal : it is not what you expected, but what you asked)
You should try to be coherent between view names returned by controller and actual jsp file :
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
}
should forward to view WEB-INF/pages/hello.jsp (that exists according to you project image)

RequestMappingHandlerMapping.getHandlerInternal:230 - Did not find handler method for

trying to make some spring example program - constantly getting the error - it happens that my controller cannot handle /hello request. Here is debug info from log4j.
13:50:58,502 {TRACE} DispatcherServlet.initContextHolders:1018 - Bound request context to thread: org.apache.catalina.connector.RequestFacade#636f2067
13:50:58,503 {DEBUG} DispatcherServlet.doService:823 - DispatcherServlet with name 'springtest' processing GET request for [/springtest_2/hello]
13:50:58,504 {TRACE} DispatcherServlet.getHandler:1088 - Testing handler map
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#7bab2c3] in DispatcherServlet with name 'springtest'
13:50:58,504 {DEBUG} RequestMappingHandlerMapping.getHandlerInternal:220 - Looking
up handler method for path /hello
13:50:58,504 {DEBUG} RequestMappingHandlerMapping.getHandlerInternal:230 - Did not find handler method for [/hello]
13:50:58,504 {TRACE} DispatcherServlet.getHandler:1088 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#722e242b] in DispatcherServlet with name 'springtest'
13:50:58,505 {TRACE} BeanNameUrlHandlerMapping.getHandlerInternal:127 - No handler mapping found for [/hello]
13:50:58,505 {WARN} PageNotFound.noHandlerFound:1108 - No mapping found for HTTP request with URI [/springtest_2/hello] in DispatcherServlet with name 'springtest'
13:50:58,505 {TRACE} DispatcherServlet.resetContextHolders:1028 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#636f2067
13:50:58,505 {DEBUG} DispatcherServlet.processRequest:966 - Successfully completed request
13:50:58,506 {TRACE} XmlWebApplicationContext.publishEvent:332 - Publishing event in WebApplicationContext for namespace 'springtest-servlet': ServletRequestHandledEvent: url=[/springtest_2/hello]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[springtest]; session=[null]; user=[null]; time=[9ms]; status=[OK]
web.xml from my project.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring3-Hibernate DEMO</display-name>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springtest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springtest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springtest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
spring-test-servlet.xml - dispatcher file from my project
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<mvc:annotation-driven />
<context:component-scan base-package="com.denmroot.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
ControllerMain.java - from my project
package com.denmroot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String helloworld (ModelMap model) {
model.addAttribute("message", "Hello World !!! Spring Output");
return "hello";
}
}
For me, I had a typo in the Spring config file which points to the package:
Was:
<context:component-scan base-package="com.something.web.controlers" />
Fixed with correct spelling:
<context:component-scan base-package="com.something.web.controllers" />
ControllerMain.java requires a #RequestMapping("/") statement after the #Controller.
Change:
#Controller
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
....
....
To:
#Controller
#RequestMapping("/")
public class ControllerMain {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
....
....
....
Actually the log says everything:
Or change your url from client to just /hello path
Or map your #Controller with:
#Controller
#RequestMapping("/springtest_2")
public class ControllerMain {
#Controller
annotation is missing for your controller class.

Categories