SpringMVC Getting Started Demo

1.MVC process

  1. MVC pattern
graph TD A (Client) -> | requesting request | B (Controller \ Servlet) B -> C (Model \ Dao, bean) C -> D (DB \ Mysql) D -> CC -> BB -> | render to | E (view \ JSP) E -> | response | A
  1. SSM: SpringMVC (complete package, and the logical data page jump), Spring, Mybatis (persistence framework ORM-Object, Relative, Mapping Object-Relational Mapping)
graph TD A (Client) -> | SpringMVC | B (Controller \ Servlet 1. 2. Package reception parameters Parameter \ dao 4. Invoke the bean objects jump) B -> | Spring | C (business layer) C -> | Mybatis | D (Model \ Dao, Bean)

2. SpringMVC process

graph TD A [client] -> B (Controller \ Servlet \ DispatcherServlet) B -> C {HandlerMapping} C -> D [controller1] C -> E [controller2] C -> F [controller3] D -> G [ModelandView] E -> G F -> G G -> H [ResoluteView] H -> A

The core SpringMVC also Servlet

ModelAndView role is to save the data, jump page

ResoluteView view resolver

Step Analysis:

  1. Customer request
  2. DispatcherServlet reach web.xml file to see if meets the requirements of url
  3. DispatcherServlet will see springmvc configuration file, find HandleMapping
  4. HandleMapping will find the appropriate controller based on the name
  5. Method execution controller class
  6. According to modelandview returned to find springmvc view resolver configuration file
  7. The view resolvers viewName and prefix and suffix made a mosaic, the mosaic display a page to the client

3. Write a simple SpringMVC of Demo

  1. Introducing SpringMVC related jar package (because it is simple demo, so only need to use the package is introduced)

  1. DispatcherServlet configured in the main configuration file
<servlet>
	<servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

When accessed in the browser Controller first letter capitalized, all other lowercase, can be omitted Controller

  1. Configuration profiles SpringMVC
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns: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">

    <!-- <bean name="/my.do" class="com.alibaba.wlq.controller.Mycontroller"></bean> -->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
    <bean class="com.alibaba.wlq.controller.MyController1"></bean>
    <bean class="com.alibaba.wlq.controller.MyController2"></bean>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name = "prefix" value="/WEB-INF/view/"/>
		<property name = "suffix" value=".jsp"/>
	</bean>

</beans>
  1. controllar class
package com.alibaba.wlq.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class MyController1 extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("login");
		mv.addObject("username","Hello World!");
		System.out.println("111111111111111");
		return mv;
	}

}
package com.alibaba.wlq.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class MyController2 extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("login");
		mv.addObject("username","Hello World!");
		System.out.println("2222222222222222");
		return mv;
	}

}

4. Common types of HandlerMapping

  1. BeanNameUrlHandlerMapping: find the corresponding Controller class The name tag bean
  2. ControllerClassNameHandlerMapping: Find the corresponding Controller class based on the name of the controller class
<!--根据controller类的名字查找对应的Controller-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<!--注册自己的controller类-->
<bean class="com.alibaba.wlq.controller.Mycontroller"></bean>

3. SimpleUrlHandlerMapping: The id of the corresponding query class Controller

<!--这里包含了id与url地址的映射关系-->
<bean class="org.springframework.web.servlet.handle.SimpleUrlHandlerMapping">
    <property name = "mappings">
        <props>
            <prop key="/a.do">my1</prop>
            <prop key="/b.do">my2</prop>
        </props>
    </property>
</bean>
<!--注册自己的controller类-->
<bean id = "my1" class = "com.alibaba.wlq.controller.MyController1"></bean>
<bean id = "my2" class = "com.alibaba.wlq.controller.MyController2"></bean>
  1. Annotation instead of using the configuration information (jar package need to import aop)
<?xml version="1.0" encoding="UTF-8"?>
	
	<beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/mvc
    	http://www.springframework.org/schema/mvc/spring-mvc.xsd
   	 	http://www.springframework.org/schema/context
   	 	http://www.springframework.org/schema/context/spring-context.xsd">
	<!--1.包扫描:扫描注解所在的包(controller类所在的包)-->
	<context:component-scan base-package="com.alibaba.wlq.controller">
	</context:component-scan>
       
    <!-- 2.开启注解驱动-->
	<mvc:annotation-driven />
    
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name = "prefix" value="/WEB-INF/view/"/>
		<property name = "suffix" value=".jsp"/>
	</bean>

</beans>

controller class:

package com.alibaba.wlq.controller;

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

@Controller
public class MyController3 {
	
	
	@RequestMapping("/list.do")
	public String list() {
		System.out.println("查询所有");
		return "login";
	}
	
}

The completion of reception parameters to SpringMVC

Guess you like

Origin www.cnblogs.com/wuliqqq/p/11449469.html