Kostyantyn Panchenko Kostyantyn Panchenko - 5 months ago 39
Java Question

Problems with controller mapping in Spring MVC

There are similiar topics, but they all use xml configuration files. The reason why I'm writing this question is that I'm using annotations.

I experience problems running my app:


  • getting “WARN org.springframework.web.servlet.PageNotFound - No
    mapping found for HTTP request with URI …” when trying to setup
    Spring servlet

  • getting error 404 when trying to run it on server



Here is my code (package and imports are skipped):

1) initializer

public class WebInitializer implements WebApplicationInitializer{

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx =
new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);

ctx.setServletContext(servletContext);

ServletRegistration.Dynamic servlet =
servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}


2) app config

@Configuration
@ComponentScan("ua.kiev.prog")
@EnableWebMvc
public class AppConfig {
@Bean
public EntityManager entityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("AdvJPA");
return emf.createEntityManager();
}

@Bean
public AdvDAO advDAO() {
return new AdvDAOImpl();
}

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

@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
}


3) controller

@Controller
@RequestMapping("/Advertisement")
public class MainController {

@Autowired
private AdvDAO advDAO;

@RequestMapping("/")
public ModelAndView listAdvs() {
return new ModelAndView("index", "advs", advDAO.list());
}

@RequestMapping(value = "/add_page", method = RequestMethod.POST)
public String addPage(Model model) {
return "add_page";
}

@RequestMapping(value = "/search", method = RequestMethod.POST)
public ModelAndView search(@RequestParam(value="pattern") String pattern) {
return new ModelAndView("index", "advs", advDAO.list(pattern));
}

// more code goes here
}


The controller is mapped to /Advertisement, so app should be available at URL localhost:8080/Advertisement/ but it isn't. When I change mapping in annotation to "/" - it becomes available at localhost:8080/Advertisement/. How can it be?
And when I change it back to "/Advertisement" - the same probleb accurs (error 404 and exception "No mapping found for HTTP request with URI …")

So, where I've made a mistake in my code?
Or maybe the problem is in Eclipse/TomCat/Maven?

Source - https://github.com/KostyantynPanchenko/prog.kiev.ua.lesson09.adv

Answer

You should change mapping

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

    @Autowired
    private AdvDAO advDAO;

    @RequestMapping("/Advertisement")
    public ModelAndView listAdvs() {
        return new ModelAndView("index", "advs", advDAO.list());
    }

The mistake that a mapper used the value from the annotation to match the request URL, and it can't match the last slash. Note, it should not happen in the above code.