I am using a spring(version 3.2.5) annotation driven mvc application, basically I need be able to catch any wrong url to a single method then display an error message. For instance, if my correct url www.url.valid/login
maps to a controller, if the user mistakenly insert loginw or any invalid url it should map to a default controller.
I have tried the following way:
package com.controller;
import static com.mns.utils.Constants.REGISTER_FAIL;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DefaultSpringController {
@RequestMapping
public String forwardRequest(final HttpServletRequest request) {
return "redirect:/" + REGISTER_FAIL;
}
}
My Application context is as follows:
<!-- Default to DefaultSpringController -->
<bean id="defaultSpringController" class="com.controller.DefaultSpringController">
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="defaultHandler" ref="defaultSpringController"/>
</bean
But kept getting the following error:
nested exception is java.lang.ClassNotFoundException: com.controller.DefaultSpringController
I am not sure what I am missing here, and the controller is in the correct package.
Any idea how I can achieve it please?
Thanks in advance
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.
RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.
@RequestMapping With Multiple URIs You can have multiple request mappings for a method. For that add one @RequestMapping annotation with a list of values. As you can see in this code, @RequestMapping supports wildcards and ant-style paths.
It look like, you want a global page not found handler.
@ControllerAdvice
public class GlobalExceptionContoller {
@ExceptionHandler(NoHandlerFoundException.class)
public String handleError404(HttpServletRequest request, Exception e) {
return "pageNotFoundViewNameGoesHere";
}
}
You can set config at web.xml
for invalid url
. Note that invalid url
usually throw the 404
error page.
<error-page>
<error-code>404</error-code>
<location>/error/error.jsp</location>
</error-page>
If it's not enough, you can go your controller method from this error page. But I usually use this config for error page instead of server side code. There have many other alternatives ways depend on your technologies usage but this way can reduce your code.
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