1

In the last days I read number of articles about Spring MVC and DispatcherServlet. For a better understanding I've started developing an annotation based MVC web application with two DispatcherServlet. dispatcherHtml for html and dispatcherJsp for jsp requests. I know html files are static, but just for practice.

ApplicationInit

public class ApplicationInit extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

    @Override
    protected String[] getServletMappings() {
        return new String[0];
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        servletContext.addListener(new ContextLoaderListener(rootContext));

        // dispatcherHtml
        AnnotationConfigWebApplicationContext webContextHtml = new AnnotationConfigWebApplicationContext();
        webContextHtml.setParent(rootContext);
        webContextHtml.register(HtmlConfig.class);
        ServletRegistration.Dynamic dispatcherHtml = servletContext.addServlet("dispatcherHtml", new DispatcherServlet(webContextHtml));
        dispatcherHtml.setLoadOnStartup(1);
        dispatcherHtml.addMapping("*.html");

        // dispatcherJsp
        AnnotationConfigWebApplicationContext webContextJsp = new AnnotationConfigWebApplicationContext();
        webContextJsp.setParent(rootContext);
        webContextJsp.register(JspConfig.class);
        ServletRegistration.Dynamic dispatcherJsp = servletContext.addServlet("dispatcherJsp", new DispatcherServlet(webContextJsp));
        dispatcherJsp.setLoadOnStartup(1);
        dispatcherJsp.addMapping("*.jsp");
    }
}

HtmlConfig

@ComponentScan("org.spring.webmvc.configuration.html")
@EnableWebMvc
@Configuration
public class HtmlConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/html/");
        viewResolver.setSuffix(".html");
        return viewResolver;
    }
}

HtmlController

@Controller
public class HtmlController {

    @RequestMapping("/index.html")
    public String index() {
        return "index";
    }
}

JspConfig

@ComponentScan("org.spring.webmvc.configuration.jsp")
@EnableWebMvc
@Configuration
public class JspConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

JspController

@Controller
public class JspController {

    @RequestMapping("/index.jsp")
    public String index() {
        return "index";
    }
}

Of course, both index.html and index.jsp files are created in the correct folder under the WEB-INF directory. Nevertheless, if I try to call the /index.html or index.jsp pages I get a no mapping found warning message.

WARNING [http-apr-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/html/index.html] in DispatcherServlet with name 'dispatcherHtml'

WARNING [http-apr-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'dispatcherJsp'

So both dispatcher servlets are added and controller methods are running as expected, but for some reason the pages cannot be loaded. Are there any ideas what did I wrong?

I tried to add the /WEB-INF/html/* and /WEB-INF/jsp/* folders to the mapping, but it didn't resolve the issue.

Dimnox
  • 441
  • 1
  • 6
  • 13

0 Answers0