Monday, February 7, 2011

Fix “no mapping found” in Spring with DefaultAnnotationHanlderMapping and AnnotationMethodHandlerAdapter


Follow trongbang86 on Twitter

I tried annotated controllers in Spring a couple of days ago. I’m a fan of Java Annotation so I was very keen on this. However, I found it really frustrating because I couldn’t use expression language (EL) such as ${objectName.attribute} in the JSP file as I normally do. After researching sometimes, I found the real problem was that the control flow didn’t go through my action class.
It’s easier to demonstrate what I’m talking by using some visualization here.
This is my controller in Java. The idea is very straightforward. Let’s put some variable in the model. And we can reuse it in the JSP file by EL.



And this is my test.jsp file:


The User class is just simple like this:



The funny thing is that I copied exactly the code from the Spring showcase which I thought that it must work for me.



What I copied from the showcase was some configurations in web.xml and the spring bean configuration file.
This is my mapping of DispatchServlet in web.xml:



My Spring Bean configuration is just to tell where the base package is:



But when I ran the application, the DispatchServlet couldn’t find the URL mapping in the Controller file:




This is my log file:

So we can notice the line: PageNotFound – No mapping found for HTTP request with URI [/simple] in DispatchServlet with name “appServlet”
Now let’s take a look again what I declared in my controller file:

After doing some research, I found out that I should use DefaultAnnotationHanlderMapping and AnnotationMethodHandlerAdapter in my Spring configuration file.

Even though they said that we just have to use DefaultAnnotationHanlderMapping when we declare some other HandlerMapping, it couldn’t work for me. I didn’t put any other HandlerMappings there. But anyway this is the way I got it working for me. Many others also have to put it like this. Hope it work for you guys! J



Follow trongbang86 on Twitter

No comments:

Post a Comment

There was an error in this gadget