SpringMVC- early learning

Traditional MVC pattern

Full Name Model View Controller, is a model (model) - abbreviation controller (controller) - the view (view)

Model (model) is a portion for processing the application logic of the application data.
  Model objects are usually responsible for accessing data in the database.
View (View) is a partial processing data applications.
  Usually view is created based on the model data.
Controller (Controller) is a part of the application processing user interaction.
  Typically the controller is responsible for reading data from the view, a user input control, and the transmission data model.
 
SpringMVC
FIG general process

We need to import some of the core jar package

 

 

 

 DispatcherServlet configuration in web.xml

<servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
        
    </servlet-mapping>

The default will go to look under WEB-INF (servlet-name) -servlet.xml

You may be used as follows (filename needs and (servlet-name) the same value)

 <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>

The next configuration profiles SpringMVC

1. Default (according to the Bean name)

2.SimpleUrlHandlerMapping (The id)

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/a.do">my1</prop>
                <prop key="/b.do">my2</prop>
            </props>
        </property>
    </bean>
    <bean id="my1" class="com.zhiyou100.xf.controller.MyController1">
        
    </bean>
    <bean id="my2"  class="com.zhiyou100.xf.controller.MyController2">
        
    </bean>

3.ControllerClassNameHandlerMapping (except the last Controller class name in lowercase)

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>

Corresponding Controller

 

 4. Use annotations (aop requires additional package)

        <! - scan package -> 
    < context: Scan-Component
         Base-Package = "com.zhiyou100.xf.controller.annotation" > 
    </ : Component context-Scan > 
    <! - Open annotation -> 
    < MVC : Annotation-Driven />    

controller

@Controller
public class MyController {
    @RequestMapping("/login.do")
    public String login(String name) {
        System.out.println("登陆");
        return "login";
    }
    @RequestMapping("/exit.do")
    public String exit(String sex) {
        System.out.println("退出");
        return "login";
        
    }
    @RequestMapping("/select.do")
    public String select() {
        System.out.println ( "Query");
        return "login";
    }
}

Springmvc process.

  1. The requesting client. http: // localhost: 8080 / springmvc-01 / my.do
  2. Reach web.xml file DispatcherServlet. View meets the requirements of url
  3. DispatcherServlet will query springmvc profile. Find HandlerMapping.

  Find the appropriate controller based on the name of the bean.

 

  4. MyController find my class. The implementation of the class handleRequestInternal method.

 

 

 

  The return modelAndView viewresolver springmvc looking profile.

   

 

 

  ViewName and the prefix and suffix made a mosaic. The show splicing of the page to the client.

 

##########################################

Receiving parameters SpringMVC

@Controller 
@RequestMapping ( "/ User" )
 public  class MyController by { 
    @RequestMapping ( "/login.do") // path corresponding 
    public String Login (String name) {// parameter name and must request the same parameter name 
        System.out .println ( "login" );
         return "login" ; 
    } 
    @RequestMapping ( "/exit.do" )
     public String exit (the User U) {// parameter name attribute names and the same request 
        System.out.println ( "exit" ) ;
         return "Login" ; 
        
    } 
    @RequestMapping ( "/ SELECT.do")
    public String select() {
        System.out.println("查询");
        return "login";
    }
}

 

Guess you like

Origin www.cnblogs.com/accc111/p/11449811.html