Ok so I have encountered the fairly common errror:
WARNING: No mapping found for HTTP request with URI [/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'app'
I'm looking at the existing answers and I've not seen a really good explanation of how the components interact. Seeing as I can't solve my issue based on the existing answers, I'm hoping someone can provide a detailed explanation of the function of DispatcherServlet
and Resolver
s.
Developing in Eclipse, I have the following structure:
/src/com/whiuk/philip/web/controller/IndexController.java
/WebContent
/WebContent/WEB-INF
/WebContent/WEB-INF/web.xml
/WebContent/WEB-INF/app-servlet.xml
/WebContent/WEB-INF/jsp/index.jsp
Eclipse Deployment Assembly means it deploys as follows:
/src -> WEB-INF/classes
/WebContent -> /
/ivy.xml[*] -> WEB-INF/lib
I have a web.xml file that defines a DispatcherServlet
and a mapping to all files (/*
)
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I have a app-servlet.xml file that scans the packages and defines a InternalResourceViewResolver
:
<context:component-scan base-package="com.whiuk.philip.web" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
I have a IndexController
that has a RequestMapping for index:
@Controller
public class IndexController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index() {
return new ModelAndView();
}
}
Logs show this is registered:
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/index],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"
onto public org.springframework.web.servlet.ModelAndView
com.whiuk.philip.web.controller.IndexController.index()
Finally I have a index.jsp file.
Can someone please explain what the misconfiguration is that results in the error shown at the top and, if possible, provide a sentence or so on the purpose of DispatcherServlet, Resolvers and how they feed into Controllers.
I will try to explain the "flow" of a request in a Spring Web MVC application.
When sending a request to your application the following happens:
preHandle
methodspostHandle
method (e.g. for modifying the model)ViewResolver
. Depending on the ViewResolver the result can be jsp page, a tiles view, a thymeleaf template or many other 'Views'. In your case the ViewResolver
resolves a view name (e.g. 'myPage') to a jsp file (e.g. /WEB-INF/jsp/myPage.jsp
)afterCompletion
method)Feel free to correct me if I'am not 100% correct or if I missed something :-)
Essentially what is happening is, since you have Spring's DispatcherServlet mapped to /*
, it tends to be called for every request(which is okay), but unfortunately gets invoked even when the request gets dispatched to the JSP page (/WEB-INF/jsp/index.jsp
), instead of the containers default servlet getting invoked.
The fix that I am aware of is the following:
Map it to the default servlet path /
instead:
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
One issue that you would see with above is that the resources under the root of your webapp will unfortunately get handled by DispatcherServlet which will not know what do with it, the fix is to register a default-servlet-handler this way:
<mvc:default-servlet-handler />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With