Spring MVC No handler found - java

I'm newbie to spring mvc and trying to develop a very basic login webapp. I get the below error while running the project. I have tried almost everything and could not fix this error since last two weeks. Please can someone help me.
May 21, 2013 2:37:12 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringWeb/WEB-INF/jsp/loginnn.jsp] in DispatcherServlet with name 'spring'
My jsp pages resides under WEB-INF/jsp. The method loginpage in my controller is gettting invoked but the view name is not being rendered and resolved. Greatly appreciate your help.
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" 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>testspring</display-name>
<servlet>
<servlet-name>frontcontrol</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontcontrol</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
frontcontrol-servlet.xml
<context:component-scan base-package ="com.shell.spring.testspringapp">
</context:component-scan>
<bean id ="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Controller class
#Controller
public class Firstcontrol {
#RequestMapping(value="/")
public ModelAndView invokeme(Model m)
{
ModelAndView mav=new ModelAndView();
mav.setViewName("result");
System.out.println("In method");
return mav;
}
#RequestMapping(value="/submit" ,method=RequestMethod.GET)
public String submit(Model m)
{
System.out.println("In submitmethod");
return "submit";
}
}

Since you're mapping your DispatcherServlet to '/' try adding <mvc:default-servlet-handler/> to your spring-servlet.xml

I saw same error, turned out that you need enable MVC annotations separately
<mvc:annotation-driven/>
In addition to
<context:Annotation-config/>

<context:component-scan base-package="your base packege " />
You should check the following section in spring configuration xml. Maybe you copied it from somewhere and forgot to make it your package name. Spring will not be able to scan packages if it so and eventually show this error.

In the web.xml file the configuration file (spring-servler.xml) should be specified to be used by the dispatcher servlet, since your not using the conventional name, which is the [servlet-name]-context.xml.
Since Spring cannot find the configuration file for the dispatcher servlet the viewresolver is never registered. I assume that 'spring-servler.xml is located within the WEB-INF folder in my example, so you may need to adjust.
Also notice I switched the servlet mapping to / which helps if you need to resolve static resources, since / acts as a catch all as opposed to mapping everything through the dispatcher.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servler.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
You could also try using a regular view resolver, if your not using JSTL.
Replace the jstl view resolver:
<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>
With:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Try returning a String from your controller that specifies the view name:
#RequestMapping(value="/")
public String loginpage(Model model)
{
Employee emp=new Employee();
ModelAndView mav=new ModelAndView();
model.addObject("emp", emp);
return "loginnn";
}

#sanjay and #Will Keeling are both right. It is required to use <mvc:default-servlet-handler/> (as the last handler) and enable both <mvc:annotation-driven/> and <context:Annotation-config/>

Related

Spring MVC handler mapping not working

I am very new to Spring MVC and have been following tutorials but they don't seem to be working.
I can load my index.jsp file fine but when I try to map it to the Dispatcher Servlet I keep getting resource not found errors.
HelloWorldController:
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC";
return new ModelAndView("hellopage", "message", message);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
</web-app>
spring-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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
my jsp files hellopage.jsp and welcomepage.jsp are in the folder jsp under my WEB-INF folder.
Index.jsp:
click|
click
I am trying to map hello.html to the HelloWorldController.java.
I would greatly appreciate any help as I am having problems mapping the first Spring MVC application.
The problem is with your servlet mapping, try changing it in your web.xml to:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
your url-pattern is set to *.html. Therefore only requests that ends with ".html" will be dispatched to spring mvc.
Add
<url-pattern>*.jsp</url-pattern>
to your servlet-mapping in web.js.
Can you try #RequestMapping(value="/hello",method = RequestMethod.GET ) ?
Your url pattern in the servlet-mapping is wrong.
You're saying go to the dispatcher servlet when the URL matches this:
*.html
So when you use "hello.html" the DispatcherServlet gets triggered.
However, there is no mapping for hello.html. There's only one for hello. HTML is static content, and it's not typical to serve that through a controller.
Try this:
Change the url-pattern to this:
<url-pattern>/</url-pattern>
Change the hello.html to hello.jsp
Change your method signature to return a string, and add the Model to the method.
#RequestMapping("/hello")
public String helloWorld(Model model) {
return "hello";
}
Your anchor tags should then point to the correct mapping. They'll look like this:
click|
So how does this work? These two pieces:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The above is a pretty standard. In the servlet mapping, it's saying that anything that matches that URL pattern should hit the servlet named spring you defined in the web.xml.
The view resolver is looking in the /WEB-INF/jsp/ directory for files ending in .jsp. In other words, Spring will look for /WEB-INF/jsp/<string>.jsp. So when you return a String hello in the method, it is actually going to attempt to resolve /WEB-INF/jsp/hello.jsp.
If you understand the above, you may have realized you can serve *.html files this way also. They would require their own view resolver, however.

Spring Controller Issue

Recently I've got a problem with simple Spring MVC Application, particularly with controller.
Here is my controller code snippet:
#Controller
public class MyController {
#RequestMapping(value="/", method=RequestMethod.GET)
public String m1(Model model){
return "form";
}
}
Here is my web.xml
<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/config/config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And here is bean configuration file:
<context:component-scan base-package="ua.macko.controller"/>
<mvc:annotation-driven/>
<mvc:resources location="/resources/" mapping="/resources/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
In the jsps folder I've got two jsp files: hello.jsp and form.jsp.
When I'm trying to achieve root page of application, such as http://localhost:8080/WebTest/ I'm ALWAYS getting hello.jsp REGARDLESS of what value I return in m1 method...whether it is "hello", "nothello", "iamnotapage" etc.
I am using Eclipse and Spring 3.2.8.
When you start Spring application in log you should see something like this :
INFO: Mapped URL path [/] onto handler 'welcomeController'
If you do not see your controller on this list it means you should check:
context:component-scan base-package="ua.macko.controller"
because your controller is not visible to Spring
If you see two mappings than yours is overridden.
If you do not see any mapping like this than most probably Beau Grantham is right, or some other config problem is there.
Is the path correct? I see you have jsps, try /WEB-INF/jsp/?
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

Spring MVC: How to get the right URL for controllers?

I'm a newbie to web development as well as Spring MVC. I'm testing on a silly example for the controllers, however, it seems that the controller has never been called, or at least it doesn't work as expected.
The goal is to click on a hyperlink in the "test.jsp" page and direct to "customers.jsp" page, with the controller being called, so that a message will be passed to the "customers.jsp" page.
Now I have the "customers.jsp" page showing correctly but with no transferred message displayed. The URL is http://localhost:8080/WebProject/view/customers.jsp.
Could you please help me find out the problem? I suppose it's because of the URL mapped to the controller. But is there a way that I can check the right URL to map for controllers? Thanks!
* updates at the end *
The project structure is like this:
--webapp
|--view
|--*.jsp
|--WEB-INF
|--*.xml
Here is my code:
test.jsp
<a href='<c:url value="customers.jsp"/>'>show customers</a>
or
show customers
both works properly.
customers.jsp
<h2>return: ${list}</h2>
Controller:
#Controller
#RequestMapping("/customers")
public class MenuController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
and the configuration:
web.xml
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
myDispatcher-servlet.xml
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
UPDATE for those who might see this post later :
There is indeed a problem of url mapping as indicated by #javafan and #Beri. But after changing the url to show customers I still got Error 404 at http://localhost:8080/WebProject/customers. This is in fact caused by a package scanning error, so the Controllers cannot be found. To solve this, simply change the line in the "myDispatcher-servlet.xml" file:
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
to
<context:component-scan base-package="controller" />
<context:component-scan base-package="model" />
Hope it helps :)
Javatan is right:
Try
<protocol>/<host>:<port>/WebProject/customers
Inside InternalResourceViewResolver bean definition you define prefix and suffix, those values are used to find jsp files in yur application. And have nothing in common with urls that your application has created.
Your url paths are defined inside controllers( not inside InternalResourceViewResolver).
So after application name you are using paths defined inside controllers.
You can also define #RequestMapping("/customers") on a method:)
Check if you have in your bean definition line, that looks like this:
<context:component-scan base-package="com.project.web.controllers"/>
This definition will scan for all classes inside com.project.web.controllers package (and subpackages) to find classes marked with #Controller annotation. You can check for more here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html, chapters:
17.3.1 - how to register a controller
17.2 -how web.xml looks like
I hope this will help.
Ok, I have run local example with JSP:
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">
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Controller:
#Controller
public class MenuController {
#RequestMapping( value="/customers", method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
dispatcher-servlet.xml
<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.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
You can use this code to get the url of the server:
"show customers"
And you are mapping the Controller to /customers
so the url should be:
http://localhost:8080/WebProject/customers/customers.jsp
You are directly calling customer.jsp. Instead you have to call controller which in turn pass the request to customer.jsp. in HREF attribute, you need to mention value which is given in #RequestMapping annotation in your controller.
For more detailed example, visit my blog Here

Web Service in Spring and Portlet environment

I am beginner in Web Service and using
Spring 3.0 and spring-webmvc-portlet 3.0
javax.portlet 2.0
I have controller as follows
#Controller(value = "myController")
#RequestMapping(value = "**VIEW**")
public class MyController {
// Controller logic
}
Now, I want to create Web Service using RESTful API in portlet environment.
Please guide me How can i write the Web Service which will return JSON or XML data.
I am still struggling with Web Service not getting WS called.
I am pasting my conf files
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/itemCatalog-portlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>webServiceTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webServiceTest</servlet-name>
<url-pattern>/myWebService/*</url-pattern>
</servlet-mapping>
item-portlet.xml
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.main.mypackage" />
<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="myPropertyEditorRegistrar" />
</list>
</property>
</bean>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language-ext</value>
</list>
</property>
</bean>
<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>
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
webServiceTest-servlet.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
portlet.xml
itemCatalog
org.springframework.web.portlet.DispatcherPortlet
text/html
view
content.Language-ext
Controller
#Controller
public class WebServiceTest {
#RequestMapping(value = "/myWebService/testing", method = RequestMethod.GET)
public String testMethod() {
return "HELLO WORLD ! SUCCESS";
}
}
I am trying to Hit with
localhost:8080:/myappname/myWebService/testing
Getting no result.
To create Web Service in portlet environment.
1. We need to use org. springframework.web.servlet.DispatcherServlet which is front controller for all the controllers available. All the HTTP request will be dispatched using Dispatcher servlet.
Add an entry in web.xml
<servlet>
<servlet-name>springwebservice</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springwebservice</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Please refer below link for dispatcher servlet read carefully
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
Now importantly each DispatcherServlet must have own WebApplicationContext. WebApplicationContext is nothing but an xml file comprises of controllers,view resolver,beans,etc .
Create file named springwebservice-servlet.xml in WEB-INF. springwebservice-servlet.xml is a WebApplicationContext.
Note
Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.
Make sure new WebAppicationContext is created for DispatcherServlet and configure it according to need.
Please guide me if mistaken somewhere.

Spring mapping problem

I am getting the following error:
No mapping found for HTTP request with
URI [/my-app] in DispatcherServlet
with name 'web'
My web.xml looks like:
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
And my web-servlet.xml looks like:
<bean name="myController" class="com.app.web.MyController" />
<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>
Any help / explanation would be great. Also, what should the view parameter be to new ModelAndView(?) in the controller?
My goal is to be able to hit http://localhost:8080/my-app and be routed to MyController which would then load a given jsp.
Your configuration looks fine to me. In your MyController, make sure you have a request mapping for my-app, like this:-
#Controller
public class MyController {
#RequestMapping(value="/my-app", method=RequestMethod.GET)
public String mainPage() {
return "index";
}
}
When you call http://localhost:8080/my-app, the server will return the index.jsp from /WEB-INF/jsp/ folder.
Looks like DispatcherServlet is trying to process the request for /my-app, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.
You might have something like this?
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>/*</url-pattern> </servlet-mapping>
Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>*.do</url-pattern> </servlet-mapping>
or change /* to /
Hope that helps.

Categories