Taher Galal February 2016

Request resource not found when run Spring 4 controller on Tomcat

I try to run a simple code but can't seem to get it to work and call the controller

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackage")
public class AppConfig extends WebMvcConfigurerAdapter {
    public void configureViewResolvers(ViewResolverRegistry registry) {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }

    /**
     * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc...
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

}

This is my configuration file I then have a controller in com.mypackage as follows:

@Controller
@RequestMapping("/")
public class testControler {

    @Autowired
    AppDataService appDataService;

    @RequestMapping(value = "/hello")
    public Model register(Model model){


        model.addAttribute("name", "Y");
        return model;
    }

    @RequestMapping("/")
    public String index(ModelMap model){

        Date start = new Date();
        System.out.println(start);
        List<AppData> app = appDataService.getListAppData();
        Date end = new Date();
        System.out.println(end);

        String result = "";
        for (Object a : app) {
            result+=a+" <br>";
        } 
        model.addAttribute("name", result);

        return "hello";
    }
}

and I have added the hello.jsp. When I run the project on the tomcat server through eclipse I don't get the hello.jsp but I get the resource not found, error 404. I am not sure what I am doing incorrect here as how I understand it

Answers


Lemon February 2016

1). Remove one more @RequestMapping("/") above the testControler class, because it produces double mapping since it's already there in index() method.

2). Modify your configureViewResolvers() to return ViewResolver object instead. Then you annotate it with @Bean :

@Bean
public ViewResolver configureViewResolvers() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver ;
    }

like so :

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackage")
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
   public ViewResolver configureViewResolvers() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver ;
    }

    /**
     * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc...
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

}

Post Status

Asked in February 2016
Viewed 3,255 times
Voted 14
Answered 1 times

Search




Leave an answer