Click here to Skip to main content
15,887,683 members
Home / Discussions / Java
   

Java

 
AnswerRe: Java- Multi-threading server with netbeans Pin
DevWithJava5-Apr-16 6:10
DevWithJava5-Apr-16 6:10 
Questionhelp Pin
Member 1206925622-Mar-16 1:09
Member 1206925622-Mar-16 1:09 
AnswerRe: help Pin
Richard MacCutchan22-Mar-16 1:21
mveRichard MacCutchan22-Mar-16 1:21 
AnswerRe: help Pin
Patrice T22-Mar-16 1:47
mvePatrice T22-Mar-16 1:47 
SuggestionRe: help Pin
Member 1206925622-Mar-16 1:59
Member 1206925622-Mar-16 1:59 
GeneralRe: help Pin
Richard MacCutchan22-Mar-16 3:09
mveRichard MacCutchan22-Mar-16 3:09 
AnswerRe: help Pin
Richard Deeming22-Mar-16 3:01
mveRichard Deeming22-Mar-16 3:01 
QuestionHow to handle 404 page not found exception in Spring MVC with java configuration and no Web.xml Pin
glassShot10015-Mar-16 6:13
glassShot10015-Mar-16 6:13 
I want to handle 404 page not found exception in my Spring MVC web app, I'm using SPRING 4.2.5.RELEASE, I had read several question regarding this topic but the similar questions are using a different spring java configuration.

I have a Global Exception Handler Controller class that have all my Exceptions, this class works fine but I can't handle a 404 page not found exception.

This is the approach that I take following a tutorial

1) I created a class named ResourceNotFoundException that extends from RuntimeException and I putted this annotation over the class definition
Java
@ResponseStatus(HttpStatus.NOT_FOUND)


like this:

Java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException { 

}


2) I created this method in my exception's controller class

Java
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {

    return "notFoundJSPPage";
}


But still when I put a URL that doesn't exist I get this error "No mapping found for HTTP request with URI"

The questions that I had read said that I need to enable to true an option for the Dispatcher but since my configuration it's different from the other questions and I don't have a Web.xml I couldn't apply that.

Here it's my Config.java

Java
@EnableWebMvc
@Configuration
@ComponentScan({"config", "controllers"})
public class ConfigMVC extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}


Here is my WebInitializer

Java
public class WebInicializar implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(ConfigMVC.class);
        ctx.setServletContext(servletContext);
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);


    }
}


Here is my Global Exception Handler Controller

Java
@ControllerAdvice
public class GlobalExceptionHandlerController {


    @ExceptionHandler(value = NullPointerException.class)
    public String handleNullPointerException(Exception e) {

        System.out.println("A null pointer exception ocurred " + e);

        return "nullpointerExceptionPage";
    }


    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public String handleAllException(Exception e) {

        System.out.println("A unknow Exception Ocurred: " + e);

        return "unknowExceptionPage";
    }


    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleResourceNotFoundException() {

        return "notFoundJSPPage";
    }

}


And the class I created that extends Runtime Exception

Java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException{

}

Questionhow do i fix this array out of bound index Pin
divinity0215-Mar-16 0:21
divinity0215-Mar-16 0:21 
AnswerRe: how do i fix this array out of bound index Pin
Richard MacCutchan15-Mar-16 2:40
mveRichard MacCutchan15-Mar-16 2:40 
GeneralRe: how do i fix this array out of bound index Pin
divinity0215-Mar-16 9:28
divinity0215-Mar-16 9:28 
GeneralRe: how do i fix this array out of bound index Pin
Richard MacCutchan15-Mar-16 10:37
mveRichard MacCutchan15-Mar-16 10:37 
GeneralRe: how do i fix this array out of bound index Pin
divinity0215-Mar-16 21:46
divinity0215-Mar-16 21:46 
AnswerRe: how do i fix this array out of bound index Pin
Patrice T16-Mar-16 16:26
mvePatrice T16-Mar-16 16:26 
GeneralRe: how do i fix this array out of bound index Pin
divinity0217-Mar-16 9:51
divinity0217-Mar-16 9:51 
QuestionNew To Java Pin
aahamdan13-Mar-16 4:15
aahamdan13-Mar-16 4:15 
AnswerRe: New To Java Pin
Richard MacCutchan13-Mar-16 7:13
mveRichard MacCutchan13-Mar-16 7:13 
Questionjava Pin
Member 1238766612-Mar-16 8:14
Member 1238766612-Mar-16 8:14 
AnswerRe: java Pin
Richard MacCutchan13-Mar-16 0:10
mveRichard MacCutchan13-Mar-16 0:10 
QuestionUsing recursion Pin
Member 1238223510-Mar-16 18:42
Member 1238223510-Mar-16 18:42 
AnswerRe: Using recursion Pin
Richard MacCutchan10-Mar-16 21:25
mveRichard MacCutchan10-Mar-16 21:25 
GeneralRe: Using recursion Pin
Member 1238223510-Mar-16 22:15
Member 1238223510-Mar-16 22:15 
GeneralRe: Using recursion Pin
Richard MacCutchan11-Mar-16 2:36
mveRichard MacCutchan11-Mar-16 2:36 
AnswerRe: Using recursion Pin
Patrice T16-Mar-16 15:26
mvePatrice T16-Mar-16 15:26 
QuestionDoubt regarding Java coding Pin
Sweacha Nlakshmi10-Mar-16 18:16
Sweacha Nlakshmi10-Mar-16 18:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.