SpringMVC入门Demo

Posted wuliqqq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC入门Demo相关的知识,希望对你有一定的参考价值。

1.MVC的流程

  1. MVC模式
graph TD A(客户) -->|发出请求request|B(Controller\\Servlet) B --> C(Model\\Dao,bean) C --> D(DB\\Mysql) D --> C C --> B B --> |渲染到|E(view\\JSP) E --> |响应|A
  1. SSM:SpringMVC(完成了数据的封装以及页面跳转的逻辑),Spring,Mybatis(持久化框架ORM-Object、Relative、Mapping对象关系映射)
graph TD A(客户) --> |SpringMVC|B(Controller\\Servlet 1.接收参数 2.封装参数\\bean对象 3.调用dao 4.跳转) B --> |Spring|C(业务层) C --> |Mybatis|D(Model\\Dao,Bean)

2. SpringMVC的流程

graph TD A[客户] -->B(控制器\\Servlet\\DispatcherServlet) B --> CHandlerMapping C -->D[controller1] C -->E[controller2] C -->F[controller3] D--> G[ModelandView] E--> G F--> G G-->H[ResoluteView] H --> A

SpringMVC的核心也是Servlet

ModelAndView的作用是保存数据,跳转页面

ResoluteView视图解析器

步骤解析:

  1. 客户发出请求
  2. 到达web.xml文件中DispatcherServlet查看是否符合url的要求
  3. DispatcherServlet就会查看springmvc的配置文件,找HandleMapping
  4. HandleMapping会根据名称查找相应的controller
  5. 执行controller类中的方法
  6. 根据返回的modelandview再找到springmvc配置文件中的视图解析器
  7. 视图解析器把viewName和prefix以及suffix做了一个拼接,把拼接的页面展示给客户

3.写一个简单的SpringMVC的Demo

  1. 引入SpringMVC相关的jar包(因为是简单的demo,所以只引入需要用的包)

技术图片

  1. 在主配置文件中配置DispatcherServlet
<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>

在浏览器中访问的时候Controller第一个字母大写,其他全部小写,也可以省略掉Controller

  1. 配置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类中
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. 常见的HandlerMapping的种类

  1. BeanNameUrlHandlerMapping:根据bean标签的名称找到对应的Controller类
  2. ControllerClassNameHandlerMapping:根据controller类的名字查找对应的Controller类
<!--根据controller类的名字查找对应的Controller-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<!--注册自己的controller类-->
<bean class="com.alibaba.wlq.controller.Mycontroller"></bean>

? 3. SimpleUrlHandlerMapping:根据id查询相应的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. 使用注解来代替配置信息(需要导入aop的jar包)
<?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类中:

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";
	
	

5.通过SpringMVC来完成接收参数

技术图片

以上是关于SpringMVC入门Demo的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC -- SpringMVC介绍快速入门快速入门流程

完成SpringMVC的入门案例

❤️‍Spring全家桶从入门到大神--SpringMVC入门

springmvc学习笔记-入门程序小结

SpringMVC 入门请求响应

SpringMVC从入门到精通