springMVC a learning log

A, springMVC flowchart is omitted

Second, write a simple demo to illustrate the springmvc

2.1 springMVC related to the introduction of the jar package

 2.2 DispatcherServlet configuration in web.xml

<servlet>
         <!-- springmvc的配置文件必须在【servlet-name】-servlet.xml -->
      <servlet-name>springMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!-- DispatcherServlet加载指定的springmvc的配置文件 -->
       <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC-annotation.xml</param-value>
       </init-param>
  </servlet>
  <servlet-mapping>
       <servlet-name>springMVC</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>

2.3 Configuration springmvc profile

<? XML Version = " 1.0 " encoding = " UTF-. 8 " ?> 
<Beans xmlns = " http://www.springframework.org/schema/beans " 
    xmlns: the xsi = " http://www.w3.org / 2001 / XMLSchema-instance " 
    xsi: schemaLocation = " http://www.springframework.org/schema/beans 
        HTTP: // www.springframework.org/schema/beans/spring-beans.xsd "> 
    <-! Configuring a handlerMapping if they do not have a default configuration handlerMapping handlerMapping 
        bean: for what represents a class of 
        Ctrl + the Shift + t:
    Find the source and path of a class ->Find the source and path of a class 
    <bean  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> 
   
    <!-- 注册自己的controller类  -->      
   <bean name="/my.do" class="com.zhiyou100.xz.controller.MyController1"></bean> 

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/view/"/>
         <property name="suffix" value=".jsp"/>
    </bean>
</beans>

 2.4 Create a Controller class

public class MyController1 extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.setViewName("login");// /WEB-INF/view/login.jsp
        mv.addObject("username", "Hello World!");//功能类似于setAttribute 在request中
        System.out.println("====mycontroller1====");
        return mv;
    }

}

Three, springmvc process

  1. The requesting client. http: // localhost: 8080 / springmvc -01 / my.do
  2. Reach web.xml file DispatcherServlet. Check whether the url requirements
  3. DispatcherServlet will query springmvc profile. Find HandlerMapping. Find the corresponding controller based on the name of the bean
  4. MyController find my class. HandleRequestInternal execution method in this class
  5. According to modelAndView returned looking springmvc view resolver configuration file
  6. ViewName and the prefix and suffix made a mosaic. The show splicing of the page to the client

Fourth, the common HandlerMapping

4.1 BeanNameUrlHandlerMapping according bean to find the corresponding name tags of Controller class

4.2 SimpleUrlHandlerMapping The bean 's id to find the corresponding Controller class.

4.3  The ControllerClassNameHandlerMapping According controller to find the corresponding name of the class of the Controller .

Fifth, use an annotation instead configuration information

5.1 Add aop jar package

 5.2 Creating Controller class

org.springframework.stereotype.Controller Import; 
Import org.springframework.web.bind.annotation.RequestMapping; 

@Controller   // equivalent to <the bean class = "com.zhiyou100.xz.controller.annotation.MyController1"> 
public  class MyController1 { 
        @ RequestMapping ( " /list.do " ) // @RequestMapping: representation is your address to access 
        public String List () { 
            System. OUT .println ( " Search " );
             return  " the Login " ; 
        } 
        
}

Sixth, be done by springmvc acceptable parameters

com.zhiyou100.xz.controller.bean.User Import; 

@Controller 
@ RequestMapping ( " / the User " ) 
 public  class MyController1 { 
    @ RequestMapping ( " /list.do " ) // @RequestMapping: representation is your address to access 
    public String List (String name, int Age) { // parameter name and parameter must be the same as the request 
        the System. OUT .println ( " name: " + name + " ; Age: " + Age);
         return  " Login " ; 
    }
    @ RequestMapping ( " /list2.do " ) // @RequestMapping: representation is your address to access 
    public String List (the User the User) { // parameters and parameter names must request the same 
        System. OUT .println (the User);    / / parameters of the request to the name of the same entity class property name 
        return  " Login " ; 
    } 
}

note:

Garbled accept parameters. Using filters. (Custom filters may also be used springmvc provided by the filter)

Guess you like

Origin www.cnblogs.com/sitian2050/p/11450515.html