简体   繁体   中英

Problems with controller mapping in Spring MVC

There are similar 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

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.

How are you running the application? Atleast in tomcat each deployed application is served from specific context path. Context path is determined from the base file name, more on that here .

So if you're deploying Advertisement.war all requests to the app will be served from localhost:8080/Advertisement/ even though you're declaring the DispatcherServlet and Controller to /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM