简体   繁体   中英

Spring JavaConfig how to map dispatcher-servlet to something other than root

My ultimate goal is to be able to have multiple servlets running at the same time (ie: one for MVC, and one for WebServices). Unfortunately, I can't get the servlet to route to the controller when the servlet-mapping is anything other than root ("/").

I would have thought that the following URL would trigger my controller, but it doesn't.

http://localhost:8080/Sandbox/Page/Test

If I remove the '/Page' from the servlet-mapping, and the '/Page' from the URL -it works fine.

http://localhost:8080/Sandbox/Test

Web Initializer

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebApplicationConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/Page/" };
    }

    @Override
    protected String getServletName() {
        return "dispatcher";
    }
}

WebApplicationConfiguration

@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
@ComponentScan(basePackages = {"com.myapp.sandbox.business", 
                               "com.myapp.sandbox.page", 
                               "com.myapp.sandbox.utility"})
public class WebApplicationConfiguration extends WebMvcConfigurerAdapter {

    /**
     * Defines a {@link ViewResolver} as a Spring managed bean.
     * 
     * @return the viewResolver
     */
    @Bean
    public ViewResolver viewResolver() {

        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/page");
        resolver.setSuffix(".jsp");

        return resolver;
    }

    /**
     * Registers resource handlers with Spring.
     *
     * @param registry the {@link ResourceHandlerRegistry}
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/view/**").addResourceLocations("/view/");
    }
}

TestController

@Controller
@RequestMapping("/Test")
public class TestController {

    @RequestMapping({"", "load"})
    public ModelAndView load(final ModelAndView modelAndView) {

        modelAndView.setViewName("/test/test"); // improve this

        return modelAndView;
    }
}

I think that your problem is in these line

resolver.setPrefix("/WEB-INF/page");

because you use

return new String[] { "/Page/" };

and the first letter are differents.

This is a little non-intuitive, but here is what is happening when your hit http://localhost:8080/Sandbox/Page/Test :

  1. Since /Page/ is mapped to the DispatcherServlet , the task of dealing with request is correctly given to the servlet you want.
  2. Once in your DispatcherServlet , the original request is used to find a handler. In doing this /Page/Test is being compared to your Controller , which is mapped to /Test . The two don't match, and hence the server responds with a 404

Update your controller to be mapped to /Page/Test and your code should work as expected.

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