No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher' [duplicate] - spring

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I am creating a simple login page with model and controller using maven, Spring 3.1.1. I have just created a model and controller. But while running the application I get a 404 error in my browser and in console I am getting the following error:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher'
I have checked the configuration properly and I couldn't find the exact error for this.
I have changed some of the configuration. I tried putting /* in URL-Mapping but I am facing the same issue.
My Web.XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Spring MVC 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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</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" 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.concretepage.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
LoginController.Java
package com.concretepage.controller;
import javax.servlet.http.HttpServletRequest;
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 LoginController {
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
#RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
public String userCheck(ModelMap model, HttpServletRequest request) {
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
model.addAttribute("message", "Successfully logged in.");
}else{
model.addAttribute("message", "Username or password is wrong.");
}
return "redirect:success.jsp";
}
}
What's my mistake?

You have not placed the Mapping for root path '/'
change this code
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
into
#RequestMapping(value="/", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
and check the output.

Related

WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name 'SpringRest'

I am trying to hit the url but I am 404 error & in console I am getting warning as WARNING: No mapping found for HTTP request with URI [/spring-mvc/hello] in DispatcherServlet with name 'SpringRest'
I am not sure about the web.xml settings. My code is like below.
Any help would be appreaciated. Thanks in Advance..
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>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
TestController.java
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wocs.services.common.ServiceException;
import com.wocs.services.order.iface.OrderServiceIface;
import com.wocs.services.order.model.hiera.OrderHiera;
#RestController
public class TestController {
#RequestMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
SpringRest-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" >
<mvc:annotation-driven/>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package = "com.wocs" />
<import resource="classpath:/beanfactory/service-bean-config.xml" />
</beans>
It might be the #RequestMapping("/hello") as you're NOT defining what type of method is used to receive the request.
Try this
#GetMapping(value="/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> hello() throws ServiceException
{
return new ResponseEntity<>("Hello............", HttpStatus.OK);
}
You can modify your Controller class like this and tell me if it works
#Controller
public class TestController {
#GetMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
if this doesn't work you can try
#Controller
public class TestController {
#RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
public String hello() throws ServiceException
{
return "Hello............";
}
}
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
check that your web.xml file and SpringRest-servlet.xml file is in webapp/WEB-INF folder, also check that your controller java class is in src/main/java and not in src/main/test , most of the time improper directory structure causes the problem. Let me know if it still does not work.
Add this to your web.xml file:-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRest-servlet.xml</param-value>
</context-param>
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

No mapping found for HTTP request with URI [/FitnessTracker/]

I am getting an warnning in springmvc
as No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
INFO: FrameworkServlet 'fitTrackerServlet': initialization completed in 2194 ms
Feb 26, 2017 9:43:08 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
When i press url http://localhost:8080/fitnessTracker/ I am getting 404 error.
Thanks
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<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>/*</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
and my 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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
and my controller
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}
Does your component-scan base-package match the package where your HelloController is in? If not, that is the problem.
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
Configures component scanning directives for use with #Configuration
classes. Either basePackageClasses() or
basePackages() (or its alias value()) may be specified to define
specific packages to scan.
Spring does not find your HelloController and so doesn't map anything.
Otherwise you could also follow these steps.
I am wondering why you have you application configured with XML. With this tool you can start a maven project with a Spring boot application that will be, by default, already configured to build exactly what you want.
Then you only need a class with:
#RestController
#RequestMapping("/fitnessTracker")
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}
And that's it, Spring will do the rest for you ;)
In my case the problem was that i was using this url-pattern
<url-pattern>/*</url-pattern>
instead of this
<url-pattern>/</url-pattern>
and also i had to add
#RequestMapping(value = "/fitnessTracker")
under the
#Controller
annotation.
I found the solution at : https://www.baeldung.com/spring-mvc-404-error for the pattern.

The requested resource is not available error in spring mvc project

I got an error as follows :
Status report
message: /SBC/loginform.html
description :The requested resource is not available.
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/Views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml
<?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" 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>OpticareVisionHouse</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>/forms/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/resources/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</web-app>
-------------------------------------------------
LoginController.java
#Controller
#RequestMapping("loginform.html")
public class LoginController {
#Autowired
public LoginService loginService;
#RequestMapping(method = RequestMethod.GET)
public String showForm(Map model) {
LoginForm loginForm = new LoginForm();
model.put("loginForm", loginForm);
System.out.print("controller calls1");
return "loginform";
}
#RequestMapping(method = RequestMethod.POST)
public String processForm(#Valid LoginForm loginForm, BindingResult result,
Map model) {
System.out.print("controller calls2");
if (result.hasErrors()) {
return "loginform";
}
/*
String userName = "UserName";
String password = "password";
loginForm = (LoginForm) model.get("loginForm");
if (!loginForm.getUserName().equals(userName)
|| !loginForm.getPassword().equals(password)) {
return "loginform";
}
*/
boolean userExists = loginService.checkLogin(loginForm.getUserName(),
loginForm.getPassword());
if(userExists){
model.put("loginForm", loginForm);
return "loginsuccess";
}else{
result.rejectValue("userName","invaliduser");
return "loginform";
}
}
------------------------------------------------
index.jsp
<% response.sendRedirect("loginform.html"); %>
------------------------------------------------
index.jsp is there at webContent.
loginform.jsp is at webContent->web-Inf ->Views
Your dispatcherServlet is mapping urls like: /forms/* to the spring servlet.
You are requesting a url like: /SBC/loginform.html
This request will never go to the spring servlet. You have to access: /{your app context}/forms/loginform.html in order to go to the spring url mapped. Your app context seems to be "SBC", so it will be something like: /SBC/forms/loginform.html
Another way is change the Servlet mapping to:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This way, every request on your app context /SBC/* will be routed to the spring servlet.
With this configuration, you should be able to access your controller with: /SBC/loginform.html

No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]

I tried to to load a simple example of spring mvc with a simple jsp but failed with the following error: No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]
I'm using Spring 3.2.2.
I really appreciate your help here.
I tried several thing but still it seems that I can not map my jsp page in the controller.
web.xml looks like that:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml looks like that:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorial.spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloControll.class looks like that:
package com.tutorial.spring;
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
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method= RequestMethod.GET)
public String printHello(ModelMap modelMap) {
modelMap.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Correct URL to hit your printHello handler method is
/HelloWeb/hello.jsp
I figured it out. foolish mistake.
I named my jsp "Hello.jsp" with capital 'H' and returned in my controller 'hello' with lower case.
Just changed my jsp page to be "hello.jsp".
thanks for your help.
Modify web.xml like this
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Try to access /YourAppName/hello. Then Spring-MVC will trigger the method printHello() . Then return the jsp view in /WEB-INF/jsp/.
The dispatcher servlet have to map the correct main route:
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
move jsp file to right place as defined in servlet
/WEB-INF/jsp/
The correct URL is http://localhost:8080/hello, without "/HelloWeb"

Spring MVC HTTP 404 error

I learning SpringMVC so I am followed Spring 3.0 MVC Series from HERE.
As you can see, I completed Part1, Part2, and I am right now on Part3 where I am learning how to handle forms with Spring 3 MVC.
But I get this HTTP 404 eror, when I try to run my application. Project strucutre and this error you can see at image below.
How I can fix this?
ContactController.java code:
package net.viralpatel.spring3.controller;
import net.virtalpatel.spring3.form.Contact;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
#Controller
#SessionAttributes
public class ContactController {
#RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(#ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
return "redirect:contacts.html";
}
#RequestMapping("/contacts")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}}
spring-servlet.xml code:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-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="net.viralpatel.spring3.controller" />
<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>
****index.jsp code:****
<jsp:forward page="contacts.html"></jsp:forward>
web.xml code:
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Just change contact to contacts
change
return new ModelAndView("contact", "command", new Contact());
to
return new ModelAndView("contacts", "command", new Contact());
The issue is in your forward it will check for the contact.jsp but actually you have contacts.jsp (you have suffix property as .jsp )
your index.jsp is forwarded to contacts.html.
But you spring configuration does not have mapping for /contacts.html, you have mapped /contacts instead.
You need to change the /contacts mapping to
#RequestMapping("/contacts.html")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}
localhost:8080/Spring3MVC/index.jsp As you can see, I try first to open index.jsp and then redirect to contact.jsp – Zookey 44 mins ago
I think you have it mixed up. 1) There is a typo, you say contact.jsp but the file name is contacts.jsp (file name in eclipse)
2) Where is the contacts.html file ?
I would suggest, you first return to the jsp and see if you can get your controller to return the jsp after that try redirecting to the html file after you create one.

Resources