First, the SPRINGMVC operation process
1. Client sends request to Core controller (Web.xml--dispatchservlet)
2. Core Controller Select Mapping Processor (handmapping) to submit the request to the business logic controller (action)
3. After the business logic controller processing is complete, return the Modelandview to the core controller
4. The core controller invokes the view resolver (viewresolve) to parse the Modelandview into a view
5. The core controller returns the view to the client
Second, the use of SPRINGMVC steps
1. Right-click the project name, select Add Spring capabilities, select Spring3.1, tick core and web libraries
2. Creating a core controller (Dispatchservlet) on Web. xml
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--If the name does not follow the format: Springmvc-servlet write, you can load the SPRINGMVC configuration file to the specified path--
<!--<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-config.xml</param-value>
</init-param>
<!--optional--><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3. Create a SPRINGMVC configuration file
1) The file must be placed in the Web-inf directory
2) The name of the file is: The name of the core controller-servlet, such as: Springmvc-servlet
If you do not name this format, you can also load the SPRINGMVC configuration file by configuring <init-param> to the specified path.
4. In the SPRINGMVC configuration file, provide the mapping processor (handmapping)
Handmapping: Provides rules for requesting a business logic controller (using annotations, which can be omitted)
(Can be omitted by using annotations)
<!--configuration handlermapping (Map processor)--
<!--mapped by name (name)--
<!--<bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean>-- >
<!--mapped based on Bookcontroller (the class name of the business logic controller)-
<!--<bean class= "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></ Bean>--
<!--Configure the business logic controller--
<!--<bean name= "/book.do" class= "Com.lzj.controll.BookController" ></bean>-
5. Create a business logic controller
That is to create the action, you need to inherit Abstractcontroller
(The use of annotations, you can omit, direct access to delete and change the method)
@Override extends Abstractcontroller
Protected Modelandview handlerequestinternal (HttpServletRequest req,
HttpServletResponse resp) throws Exception {
System.out.println ("Hello");
Modelandview mv = new Modelandview ("Pages/main");
Mv.addobject ("Reponse", "Springmvc");
Req.setattribute ("Reponse", "Springmvc");
return MV;
}*/
6. Configure the View resolver
<!--Configuring views View Parser--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/"/>
<property name= "suffix" value= ". jsp"/>
</bean>
Using JDBC or hibernate to connect to the database at this point, you can use SPRINGMVC.
7.SpringMVC Ways to use annotations
1) Configure the namespace:
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation=http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd
<!--enable the annotation processor--
<context:annotation-config/>
<!--enable annotations to be automatically scanned-
<context:component-scan base-package= "Com.lzj"/>
<!--enable SPRINGMVC annotation processor--
<mvc:annotation-driven/>
Specific as follows:
@Controller
@RequestMapping ("/book")
public class bookcontroller{
Bookdao dao = new Bookdao ();
(value= "/add", Method=requestmethod.post) can restrict a form to be submitted only by POST
@RequestMapping (value= "/add", Method=requestmethod.post)
Public String Add (book book) {
Dao.add (book);
return "Redirect:show.do";
}
/*
* Transmitted to the interface, you can use the HttpServletRequest Model Modelmap,
* But SPRINGMVC first read the model and MODELMAP data,
* General use of model or MODELMAP
*/
@RequestMapping ("/show")
Public String Show (model model) {
(httpservletrequest) Request.setattribute ("Bookslist", Dao.select ("from book");
Model.addattribute ("Bookslist", Dao.select ("from book"));
return "Pages/selectbook";
}
@RequestMapping ("/delete")
Public String Delete (int id) {
Dao.delete (ID);
return "Redirect:show.do";
}
@RequestMapping ("/update")
Public String Update (int id,modelmap modelmap) {
Modelmap.addattribute ("book", Dao.getobj (ID));
return "Pages/updatebook";
}
@RequestMapping ("/save")
Public String Save (book book) {
Dao.add (book);
return "Redirect:show.do";
}
}
SPRINGMVC Basic Operation