XML-free Spring 3.1 No mapping found for HTTP request
Asked Answered
A

2

7

I've scoured google, stackoverflow, and every forum I can look at for a few days and my keyboard is in dire risk of being the target of a headbutt.

I'm running a very small Spring 3.1 MVC with an XML-free setup. The problem is that when I start it up I see;

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/start.action],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.start(javax.servlet.http.HttpServletRequest)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/*],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.home(java.util.Locale,org.springframework.ui.Model)

Yet when I try to hit either of those URLs I see my logging statements inside my controller fire and then immediately get;

INFO : com.xxxxxx.info.HomeController - Welcome home! the client locale is en_US
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/jsps/home.jsp] in DispatcherServlet with name 'dispatcher'

Here are my source files.

Initializer -

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(MvcConfig.class);

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

MvcConfig -

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.xxxxxx.info")    
public class MvcConfig {
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsps/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

Controller -

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /** Simply selects the home view to render by returning its name. */
    @RequestMapping(value = "/*")
    public ModelAndView home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is " + locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);
        return new ModelAndView( "home", model.asMap() );
    }

    @RequestMapping( value = "/start.action")
    public ModelAndView start(HttpServletRequest request) {
        logger.info("Starting!");
        return new ModelAndView( "start", null );
    }
}

Changing the dispatcher mapping to "/" as many posts I found suggested seem to break it entirely. If I restart the server with "/" then nothing is reported by Spring, nothing gets mapped, just a tomcat startup log and nothing else works.

My file structure is -

| com.xxxxxx.info
    - Initializer.java
    - MvcConfig.java
    - HomeController.java
| src
    | main
        | webapp
            | WEB-INF
                | jsps
                    - home.jsp
                    - start.jsp

So it appears to hit my controller correctly but when it gets the view name it doesn't resolve to the right place. What am I missing here, this seems like something simple I've overlooked...

Aaronson answered 14/11, 2012 at 5:5 Comment(0)
S
2

I guess that the jsp are being rendered by the DispatcherServlet, it's discussed in the similar question

You should consider using a more specific mappings both in HomeController and in the WebApplicationInitializer.

Sublingual answered 14/11, 2012 at 13:6 Comment(2)
@Sarophym it's not clear what can go wrong with / mapping for the dispatcher servlet, you should first check that controller mapping works for non-root requests like hello.html.Sublingual
Thanks much for the link. The solution there was dead on. I changed my dispatcher code to dispatcher.addMapping("*.action");Aaronson
D
2

You are dispatching \* to both DispatcherServlet as well as your HomeController

dispatcher.addMapping("/*");

@RequestMapping(value = "/*")
    public ModelAndView home(Locale locale, Model model)

And i think that is causing the problem. Change your controller mapping to something like

@RequestMapping(value = "/home")
    public ModelAndView home(Locale locale, Model model)

Hope it helps.

Dichotomous answered 14/11, 2012 at 13:20 Comment(1)
This didn't fix it but I think you were onto the right problem, it turned out that changing the dispatcher mapping fixed the issue.Aaronson

© 2022 - 2024 — McMap. All rights reserved.