Initial Spring MVC and initial springmvc
1.
1. Introduction to Spring MVC
Spring MVC is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for Building Web applications. Spring can be inserted into the MVC Architecture. Therefore, when using Spring for WEB development, you can choose to use Spring MVC framework or integrate other MVC development frameworks, such as Struts1 and Struts2.
Framework editing uses policy interfaces. The Spring framework is highly configurable and contains multiple view technologies, such as assumerver Pages (JSP), Velocity, Tiles, iText, and POI. The Spring MVC Framework does not know the view used, so it does not force you to only use JSP technology. Spring MVC separates the roles of controllers, model objects, filters, and handler objects, making them easier to customize. Advantages: Lifecycle for overriding binding, validation, etc. It is easy to seamlessly integrate with other View frameworks (such as Tiles) and easy to test using IOC. It is a typical textbook-style mvc Architecture, unlike struts, which is a variant or a framework not completely based on the mvc system, for beginners or those who want to know mvc, I think spring is the best. Its implementation is a textbook! Second, it is a pure servlet system like tapestry, which is also its advantage over struts. And the framework itself has code, which seems easy to understand.
The Spring MVC framework has an MVC framework that implements the Model-View-Controller mode to effectively separate data, business, and presentation. From this perspective, Spring MVC is very similar to Struts and Struts2. Spring MVC is designed around DispatcherServlet. DispatcherServlet is responsible for distributing requests to specific handler. You can use handler mappings, view resolution, locale, and theme resolution to process requests and go to the corresponding view. Overall process of Spring MVC request processing
2. Unit Test
1. Build a testing Web Environment
<! -- Register a central processor --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <! -- Set the context parameter name --> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </init-param> <! -- Mark whether the container is instantiated at startup --> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping>
View Code
2. Define a controller class HelloController:
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class MyController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("WEB-INF/jsp/index.jsp"); return mv; }}
View Code
3. SpringMVC. xml configuration
<bean id="/happy.do" class="cn.happy.controller.MyController"></bean>
View Code
4. Here, happy. do is the id specified in XML.