Spring MVC is a Web MVC framework and is one of the mainstream web MVC frameworks today.
Spring MVC works simply as shown in the following:
The next step is to configure Spring MVC
First we configure spring MVC to introduce the relevant jar package first
We need to have a processor, the controller layer, and configure it into the IOC, and when the user accesses a path, the front-end controller intercepts a handler map to the IOC container, and if there is a match, the requestmapping accesses the method. Otherwise, a 404 exception is generated.
@Controllerpublic class HelloWorld { @RequestMapping ("hello1.action") public Modelandview Hello () { Modelandview Mav = new Modelandview (); Mav.addobject ("Data", "Hello"); Mav.setviewname ("Hello"); return MAV;} }
and scan it into an IOC container.
<Context:component-scanBase-package= "Package Com.xt.crud"></Context:component-scan><Mvc:default-servlet-handler/> <Mvc:annotation-driven></Mvc:annotation-driven><Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"value= "/views/"></ Property> < Propertyname= "suffix"value= ". jsp"></ Property> </Bean>
<Mvc:default-servlet-handler/> means that the processing of static resources that do not match the controller to the Web container, such as CSS,JS, is given
But when we hand over the processing of static resources to the Web container, you will find that the paths in the requestmapping we write are inaccessible because when we do not add this tag, the framework is registered with the default
Annotationmethodhandleradapter This class, he will handle annotations in spring MVC, matchingrequestmappingIn the path, but when we add the<mvc:default-servlet-handler/> ,
The front controller intercepts the interception path and resource request to the default processor, which cannot process the path in the controller, and<mvc:annotation-driven > Will register a requestmappinghandleradapter,
and processing after the default processor finishes processing static resourcesrequestmappingThe path in the.
<BeanClass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name= "prefix" value= "/views/"></Property> <property name= "suffix" value= ". jsp"></Property> </ Bean>
Can simplify the path we return to the view layer, such as we want to return to view/success.jsp, configure the above content, we only need to write success.
Next configure the front-end controller (dispatcherservlet) to open Web. xml
Locate the Dispatcher.class under the SPRING-WEBMVC package and configure it to the servlet tag
<servlet> <Servlet-name>Dispatchar</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring/crud.xml</Param-value> </Init-param></servlet>
When configuring the front-end controller, We need to initialize the Contextconfiglocation property in Org.springframework.web.servlet.DispatcherServlet and assign him to the path of the IOC container we just wrote,
So that he can compare our request with the requestmapping in the controller in the IOC. There is also a <load-on-startup></load-on-startup> tag in the servlet tag, which is used to set up multiple Servlets
Interception of priority, the smaller the number the higher the priority, for example, the <load-on-startup>0</load-on-startup> servlet will be more than <load-on-startup>1</ Priority of load-on-startup> implementation.
Next, configure the Intercept path for the front controller
< servlet-mapping > < Servlet-name > Dispatchar</servlet-name> <Url-pattern> /</url-pattern></servlet-mapping >
/means intercept all paths, including static resources
The above is the configuration process for the front-end controller (Dispatcherservlet), which will share the restful, automatic boxing and redirection in spring MVC next week
Analysis of the configuration and dispatcherservlet of Spring MVC