在最后几天,我阅读了有关Spring MVC和DispatcherServlet的文章数量。为了更好地理解,我开始使用两个DispatcherServlet开发基于注释的MVC Web应用程序。用于html的dispatcherHtml和用于jsp请求的dispatcherJsp。我知道html文件是静态的,但只是为了练习。
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";
}
}
当然,index.html和index.jsp文件都是在WEB-INF目录下的正确文件夹中创建的。不过,如果我尝试调用/index.html或index.jsp页面,我会收到一条没有映射的警告消息。
警告[http-apr-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound没有映射 找到带有URI [/WEB-INF/html/index.html]的HTTP请求 DispatcherServlet,名称为&#39; dispatcherHtml&#39;
警告[http-apr-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound没有映射 找到带有URI [/WEB-INF/jsp/index.jsp]的HTTP请求 DispatcherServlet,名称为&#39; dispatcherJsp&#39;
因此,添加了两个调度程序servlet,并且控制器方法按预期运行,但由于某种原因,无法加载页面。有什么想法我错了吗?
我尝试将/ WEB-INF / html / *和/ WEB-INF / jsp / *文件夹添加到映射中,但它没有解决问题。