springMVC study notes (1): springmvc development process

One, mvc design pattern, a pattern for creating Web applications

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The official name "Spring Web MVC" comes from the name of its source module (spring-webmvc), but is more commonly referred to as " Spring MVC ".
In other words: Spring mvc is a framework based on the web layer (request processing and response sending b/s).
Tips: sturts1, struts2, spring mvc, spring webFlux, etc. all belong to the web layer framework

Two, the traditional b/s architecture is different from the spring mvc architecture

mvc模式:m (model ) v(view–jsp,html) c (control–servlet)

1) Traditional b/s architecture

Client------request-----controller (servlet)----->encapsulated data model
------->response-----------> jsp-----------------|
Disadvantages: The servlet production cycle is long, and a large number of servlets are produced. Occupies a lot of memory.

2) b/s architecture of spring mvc

advantage:

  1. There is only 1 servlet.
  2. Clear division of labor:

Central processing unit (servlet): accepts requests, assigns tasks, and sends responses.
Mapper : According to the url, determine who to look for (processor), what to do (what business), and tell it to the boss.
Adapter : According to the mapper result, find the corresponding processor, complete the task, and return the result to the boss.
View parser : parsing the result returned by the processor (model view)—which jsp to return, and what data to render inside.

Insert picture description here

Three, spring mvc development process

Step 1: Package required by mybatis spring mvc (spring+ spring-web package+spring-webmvc)

spring-web-5.0.14.RELEASE.jar
spring-webmvc-5.0.14.RELEASE.jar

Step 2: Web.xml configuration central processing unit (a servelt written by spring)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Springmvc01</display-name>
  
  <!--  Tomcat容器来加载spring 和spring mvc配置-->
  <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 三、spring的编码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  
  <!--二、 spring mvc的配置==>中央处理器(1个老大与3个小弟) -->
    <display-name>nd-springmvc01-base</display-name>
    <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:spring-web.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- / 中央处理器处理所有请求:包括jsp html 注意:不能写/*,否则报错 *.do 中央处理器处理url中以.do结果 比如: 
            http://127.0.0.1:8080/easybuy/index.do 被处理 http://127.0.0.1:8080/easybuy/index.jsp 
            不被处理 -->
        <!-- <url-pattern>*.do</url-pattern> -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Step 3: Configure spring-web.xml (mapper, adapter, view resolver)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        ">
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.lq.web"/>
	<!-- 1.映射器:HandlerMapping  url==>handler -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	<!-- 2.适配器: HandlerAdatper   调用 Handler==>Controller-->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	<!-- 3.视图解析器: -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 解析jstl标签 -->
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<!-- 动态页面的前缀 -->
		<property name="prefix" value="/WEB-INF/" />
		<!-- 动态页面的后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

Step 4: Define the processor
@Controller
@requestMapping

package com.lq.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lq.entity.Users;
import com.lq.service.IUsersService;

//http://localhost:8080/nd-springmvc01-base2/user/list
@Controller// 等价于@Component
@RequestMapping("/user")
public class UserController {
    
    
	
	@Autowired
	private IUsersService iUsersService;
	
    @RequestMapping("/list")
    public ModelAndView list() {
    
    
        System.out.println("调用UserController的list方法");
        //查询分页查询users的数据(Model)
        List<Users> users = iUsersService.list();
        
        //返回model(users)AndView(users.jsp)
        ModelAndView mav=new ModelAndView();
        mav.addObject("users",users);
        mav.setViewName("users");
        return mav; //"user.jsp"
    }
    
    @RequestMapping("/to_edit")
    public String toEdit(Model model,Integer id) {
    
    
    	Users userDetail = iUsersService.getUser(id);
    	model.addAttribute("userDetail", userDetail);
    	
    	return "edit_user";
    }
    
    @RequestMapping("/edit")
    public void edit() {
    
    }
    
    @RequestMapping("/login")
    public String login() {
    
    
    	System.out.println("用户登录成功");
    	return null;
    }
    
    
}

[Supplement] The
@Component annotation is refined in the later version of spring:
1) @Controller ----web layer
2) @Service -----business layer
3) @Repository -----persistence layer spring data jpa

Next chapter: springMVC study notes (two): controller parameters

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/108983919