1. SpringMVC与Servlet的关系
SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在SpringMVC的controller中,完全按照Servlet的方式来写代码,同时还享受SpringMVC提供的方便。所以我们完全没有理由再使用Servlet了。
2. SpringMVC 返回 html 视图页面
SpringMVC的controller一般我们可以配置返回:jsp, json, Velocity, FreeMarker, XML, PDF, Excel等等视图。那么如何返回到 html 视图呢?当然一般我们没有这种必要,但是有一些奇葩场景还是存在的。
上面我们知道SpringMVC没有限制对Servlet有任何的限制,我们完全可以在controller中写servlet形式的代码,而我们知道,我们在Servlet中可以通过 转发和重定向到 html 类型的页面,所以我们也可以在SpringMVC的controller中利用request的转发和response的重定向到html页面,也可以利用PrintWriter直接输出html字符流。这些Servlet中的用法,我们完全可以在SpringMVC中使用。
@RequestMapping(value="/htmlView")public void htmlView(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException{//...//request.getRequestDispatcher("index.html").forward(request, re