SpringMVC个人学习笔记

SpringMVC笔记

前言:

本笔记是个人学习SpringMVC时记录的简单笔记,以供日后复习或使用时查看!!!

仅供参考!!

MVC回顾

1.Controller控制器:

  • 获取表单数据
  • 调用业务逻辑
  • 转向指定页面

2.Model模型:

  • 业务逻辑
  • 保存数据的状态

3.View视图:

  • 显示页面

SpringMVC初步

1.SpringMVC围绕DispatcherServlet,它也是Servlet,负责接受请求和调度

SpringMVC执行原理:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-57zyf5dL-1685797052354)(C:\Users\cxz\Desktop\SpringMVC执行原理.png)]

具体步骤:

  • 1:前端提出请求的URL
  • 2:HandlerMapping根据URL中的hello找到处理器(controller)
  • 3、4:找到的控制器再返回给DispatcherServlet
  • 5、6:HandlerAdapter是适配器,去执行Handler,Handler再让具体的Controller执行
  • 7、8:controller再返回ModelAndView给HandlerAdapter,再传给DispatcherServlet
  • 9、10:DispatcherServlet传给视图解析器,解析器再将ModelAndView进行数据解析,找到视图的名字并传给DispatcherServlet
  • 11:DispatcherServlet根据具体的视图解析器解析的结果,调用具体的视图

Controller

1。标准的springmvc-servlet.xml文件:

<?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:mvc="http://www.springframework.org/schema/mvc"
       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 https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	
    <!--三个不变的配置:-->
    <!--包扫描:使注解的controller类能被扫描-->
    <context:component-scan base-package="com.cxz.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
	
    <!--试图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>

    </bean>
</beans>

2.web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
	
    <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:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3.控制器类的实现方式1:实现Controller接口(该方法较老,尽量不使用!)

4.控制器实现方法2:使用注解@Controller:

package com.cxz.controller;

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

/*
     这个类会被Spring接管,这个注解的类的所有方法,如果其返回String,
     并且页面跳转,则会自动别视图解析器解析
*/
@Controller
public class ControllerTest2 {

    @RequestMapping("/test01")	//前端的url:http://localhost:8080/test01
    public String test01(Model model){

        model.addAttribute("msg","ControllerTest2....");

        return "test";  //去找"/WEB-INF/jsp/hello.jsp"
    }
}

RequestMapping

类上的@RequestMapping是父路径!

package com.cxz.controller;

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

@Controller
@RequestMapping("/c3")		//此时的url:http://localhost:8080/c3/t1
public class ControllerTest03 {

    @RequestMapping("/t1")
    public String test01(Model model){
        model.addAttribute("msg","RequestMaping!");
        return "test";
    }
}

RESTful风格

定义:资源定位的风格

url:localhost:8080/add/1/2

 @RequestMapping"/add/{a}/{b}")
    //使用@PathVariable注解,映射参数到url上去
    public String test01(@PathVariable int a, @PathVariable int b, Model model){

        int res = a+b;
        model.addAttribute("msg","res:"+res);

        return "test";
    }

方式2:使用指定方式的注解

@PostMapping/ @GetMapping/@DeleteMapping/@PutMapping

//PostMapping:
   @PostMapping("/add/{a}/{b}")    //Post方式!
    //使用@PathVariable注解,映射参数到url上去
    public String test02(@PathVariable int a, @PathVariable int b, Model model){

        int res = a+b;
        model.addAttribute("msg","res:"+res);

        return "test";
    }
}

转发和重定向

什么时候用:在没有视图解析器的时候用

接收前端参数

controller类可以接收前端的参数


@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/t1")
    /**
     * 前端的url:http://localhost:8080/user/t1?username=cxz
     *          必须是username,不能是name,因为使用了@RequestParam注解了
     */
    public String test01(@RequestParam("username") String name, Model model){

        System.out.println("接收前端参数"+name);

        model.addAttribute("msg",name);


        return "test";
    }

    /**
     * 前端的url:http://localhost:8080/user/t2?name=cxz&id=12&age=10
     *          前端提交的是一个user,参数名必须和属性名一一对应,不能拼写错误
     */
    @GetMapping("/t2")
    public String test02(User user){
        System.out.println(user);
        return "test";
    }
}

解决乱码问题

在xml中配置过滤器:

  <!--SpringMVC的乱码过滤器-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <!--注意这里是: /*-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

=Continue。。。========

日后继续更新, 仅供参考!!个人所用
aram>

<filter-mapping>
    <filter-name>encoding</filter-name>
    <!--注意这里是: /*-->
    <url-pattern>/*</url-pattern>
</filter-mapping>





=====================================Continue。。。============================================

日后继续更新, 仅供参考!!个人所用
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值