springMVC学习笔记

官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc

使用spring注解开发

步骤:
1、新建web项目(将简单的maven项目添加框架)
2、导入相关的jar包

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>

3、编写web.xml文件,注册DispatcherServlet(类似mvc中的注册servlet)

<?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">
<!--配置DispatchServlet;这个是springMVC的核心,请求分发器,前端控制器-->
<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> <!--在springMVC中,/:代表匹配所有的请求,不包括jsp页面。/*:匹配所有的请求,包括jsp-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

4、编写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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描包,让指定包下的注解生效,有ioc容器统一管理-->
<context:component-scan base-package="com.zheng.controller"/>
<!--让springmvc 不处理静态资源-->
<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> <!--BeanNameUrlHandlerMapping--> </beans>

5、创建对应的控制类,controller

6、构造前端视图和controller之间的对应

//代表这个类会被spring接管,被这个注解的类中的所有方法,如果返回值是string,并且有具体页面可以跳转,那么就会被视图解析器解析
@Controller
public class HelloController { @RequestMapping("/hello")
public String hello(Model model){
//封装数据
model.addAttribute("msg","hello springMVC");
return "hello"; }
}

7、测试

8、项目结构

使用springMVC必须配置的三部分

  • 处理器映射器
  • 处理器适配器
  • 视图解析器

注意RequestMapper说明

@Controller
@RequestMapping("c3")
public class ControllerTest3 {
@RequestMapping("/t3")
public String tset3(Model model){
model.addAttribute("msg","hello test3");
return "test";
}
}

如果RequestMapping写在类名之上,则在访问的时候需要带上它,例如/c3/t3.
一般而言只写在方法名上即可。

在测试的时候:访问出现404
1、查看控制台报的错误,缺少jar包,导入相应的jar包即可
2、jar包存在,在IDEA的项目中、添加lib依赖
【file–>project structure–>artifacts–>选中对应的项目–>Output layout–>WEB-INF–>新建lib文件夹–>点击上方的加号+添加library files所有的包到lib】

ResultFul风格

package com.zheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*; @Controller
public class ResultController { // @GetMapping("/add/{a}/{b}") //http://localhost:8080/springMVC/add/6/9
public String test1(@PathVariable int a, @PathVariable String b, Model model){
String res=a+b;//数字和字符串拼接
model.addAttribute("msg","test1"+res);
return "test"; } @RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.GET) //http://localhost:8080/springMVC/add/4/7
public String test2(@PathVariable int a,@PathVariable String b,Model model){
String res=a+b;
model.addAttribute("msg","test2:"+res);
return "test";
}
//http://localhost:8080/springMVC/add?a=2&b=4
// @RequestMapping("/add")
public String test(int a,int b, Model model){
int res=a+b;//数字和字符串拼接
model.addAttribute("msg","结果="+res);
return "test"; }
}

一般的资源访问是: //http://localhost:8080/springMVC/add?a=2&b=4
使用规范后的是://http://localhost:8080/springMVC/add/4/7

重定向和转发

package com.zheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class ModelTest { //此时视图解析器被注释掉
@RequestMapping("/m/t")
public String test(Model model){
model.addAttribute("msg","ModelTest1");
return "forward:/WEB-INF/jsp/test.jsp"; //转发的形式,没有视图解析的情形,地址栏不变
} @RequestMapping("/t")
public String test2(Model model){
model.addAttribute("msg","test2");
return "redirect:/index.jsp";//重定向,没有视图解析的情形,地址栏改变 } }

乱码问题

配置tomcat,打开tomcat文件下的conf–》server。

  • 添加【URIEncoding=“UTF-8”】到对应的文件位置
<Connector port="8080" URIEncoding="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>

后端框架学习3------SpringMVC的更多相关文章

  1. 后端框架学习1-----Spring

    Spring学习笔记 spring全家桶:https://www.springcloud.cc/spring-reference.html spring中文文档:http://c.biancheng. ...

  2. 后端框架学习-----mybatis(使用mybatis框架遇到的问题)

    1.配置文件没有注册(解决:在核心配置文件中注册mapper,注册有三种形式.资源路径用斜杆,包和类用点) <mappers> <!--每一个mapper.xml文件都需要在myba ...

  3. 后端框架学习-----mybatis(4)

    文章目录 4.解决属性名和字段名不一致的问题 4.解决属性名和字段名不一致的问题 1.问题.数据库字段名和属性名不一致,导致查出的数据部分为空 2.resultMap(用于解决数据库表中的字段和属性) ...

  4. Spring框架学习笔记(3)——SpringMVC框架

    SpringMVC框架是基于Spring框架,可以让我们更为方便的进行Web的开发,实现前后端分离 思路和原理 我们之前仿照SpringMVC定义了一个自定义MVC框架,两者的思路其实都是一样的. 建 ...

  5. 第65节:Java后端的学习之Spring基础

    Java后端的学习之Spring基础 如果要学习spring,那么什么是框架,spring又是什么呢?学习spring中的ioc和bean,以及aop,IOC,Bean,AOP,(配置,注解,api) ...

  6. 整合SSM框架必备基础—SpringMVC(下)

    在上一篇文章<整合SSM框架必备基础-SpringMVC(上)>中,胖达介绍了关于SpringMVC的诞生.优势以及执行流程等理论知识点,这篇文章打算在实操中加深一下对SpringMVC的 ...

  7. 适合普通大学生的 Java 后端开发学习路线

    大家好,我是帅地. 接下来的一段时间,帅地会总结各种技术栈的学习路线,例如 Java 开发,C++ 开发,python 开发,前端开发等等,假如你没有明确的目标,或许可以按照我说的学习路线来学习一波, ...

  8. JavaSE中Collection集合框架学习笔记(2)——拒绝重复内容的Set和支持队列操作的Queue

    前言:俗话说“金三银四铜五”,不知道我要在这段时间找工作会不会很艰难.不管了,工作三年之后就当给自己放个暑假. 面试当中Collection(集合)是基础重点.我在网上看了几篇讲Collection的 ...

  9. Java后端框架之Spring Boot详解,文末有Java分布式实战项目视频可取

    在 Java 后端框架繁荣的今天,Spring 框架无疑是最最火热,也是必不可少的开源框架,更是稳坐 Java 后端框架的龙头老大. 用过 Spring 框架的都知道 Spring 能流行是因为它的两 ...

随机推荐

  1. GreatSQL MGR FAQ

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 目录 0. GreatSQL简介 1. GreatSQL的特色有哪些 2. Gr ...

  2. 学习javascript知识

    开始学习了 努力----努力----努力 从今天开始 绝不 三天打鱼两天晒网 先把基础再巩固一下

  3. STC8H开发(十五): GPIO驱动Ci24R1无线模块

    目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...

  4. 8月Meetup | “数据调度+分析引擎”解锁企业数字化转型之路

    ​ 编辑切换为居中 添加图片注释,不超过 140 字(可选)   大数据是一种规模大到在获取.存储.管理.分析方面大大超出了传统数据库软件工具能力范围的数据集合,而大数据作为企业运转的基础,只有经过提 ...

  5. RISC-CPU设计和 FPGA 实现

    摸鱼的时候找到一份单周期和多周期的riscv的fpga实现,还是挺符合我的预期的 知乎专栏地址:https://www.zhihu.com/column/c_1530950608688910336

  6. React报错之Functions are not valid as a React child

    正文从这开始~ 总览 产生"Functions are not valid as a React child. This may happen if you return a Compone ...

  7. Luogu4391 [BOI2009]Radio Transmission 无线传输 (KMP)

    \(最小循环节\) \(=\) \(lenghth - next[lenghth]\) #include <iostream> #include <cstdio> #inclu ...

  8. BZOJ1977/LuoguP4180【模板】严格次小生成树[BJWC2010] (次小生成树)

    这道题本身思维难度不大,但综合性强,细节多 在其上浪一个早上,你的 最小生成树 树链剖分 线段树 DEBUG能力... 都大幅提升 细节与思路都在代码里面了. 欢迎hack. #include< ...

  9. Excel 运算符(二):比较运算符

    比较运算符用于对两个数据进行比较运算,其结果为 TRUE(真)或 FALSE(假). 运算符 含义 实例 结果 = 等于 =2=3 FALSE < 小于 =5<2 FALSE > 大 ...

  10. 演示RabbitMQ的交换类型

    一.Direct exchange 新建一个名为direct_exchange的Direct exchange 添加队列direct_queue1 添加队列direct_queue2 direct_e ...