【SpringMVC】基础部分

SpringMvc

  • Spring MVC 是Spring提供的一个实现了Web MVC设计模式的轻量级Web框架。

  • MVC(Model View Controller),一种用于设计创建Web应用程序表现层的模式

    • Model(模型):数据模型,用于封装数据
    • View(视图):页面视图,用于展示数据
    • Controller(Handle 处理器):处理用户交互的调度器,用于根据用户需求处理程序逻辑

三层架构

  • 表现层:负责数据展示

  • 业务层:负责业务处理

  • 数据层:负责数据操作

SpringMVC项目步骤

1 新建maven的web项目
【SpringMVC】基础部分_第1张图片
2 导入maven依赖

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.1.9.RELEASE</version>
</dependency>
<!--spring web的坐标-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.1.9.RELEASE</version>
</dependency>
<!--springmvc的坐标-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.9.RELEASE</version>
</dependency>
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

3 创建controller
在这里插入图片描述

@Controller
public class UserController {
    @RequestMapping("/save")
    public String say(){
        System.out.println("你好");
        return "a.jsp";
    }
}

4 创建spring-mvc.xml配置文件(本质就是spring的配置件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
    ">

    <context:component-scan base-package="com.xinzhi"/>
</beans>

5 web.xml中配置前端控制器

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-mvc.xml</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

6 新建a.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: 86166
  Date: 2023/9/16
  Time: 11:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>

</head>
<body>
<p>p标签</p>
${name}
</body>
</html>

7 配置tomcat
在这里插入图片描述
【SpringMVC】基础部分_第2张图片
【SpringMVC】基础部分_第3张图片
测试

参数绑定代码

//    默认类型:

    @RequestMapping("/test1")
    public ModelAndView tset1(HttpServletRequest request,ModelAndView modelAndView){
        String name = request.getParameter("name");
        System.out.println(name);
        modelAndView.setViewName("a.jsp");
        modelAndView.addObject("name",name);
        return modelAndView;
    }
//    简单类型:
    @RequestMapping("/test2")
    public String test2(String name,String age){
        System.out.println(name);
        System.out.println(age);
        return "a.jsp";
    }
//# 对象类型
    @RequestMapping("/test3")
    public String test3(User user){
        System.out.println(user);
        return "a.jsp";
    }
//# 数组
    @RequestMapping("/test4")
    public String test4(String[] name){
        System.out.println(Arrays.toString(name));
        return "a.jsp";
    }

//# list类型
    @RequestMapping("/test5")
    public String test5(@RequestParam("name") List<String> name){
        System.out.println(name);
        return "a.jsp";
    }
//类型转换
    @RequestMapping("/test6")
    @ResponseBody
    public String test6(Date time){
        System.out.println(time);
        return "bbb";
    }
  • 自定义数据绑定

    • 定义转换器
    public class MyDateConverter implements Converter {
    
        @Override
        public Date convert(String s) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = df.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }
    
    • 注解驱动,使转换器起作用
    
    
    
    
        
        
            
            
                
                
            
        
    
    
    
    

你可能感兴趣的:(java)