目录
1.1@RequestMapping
@RequestMapping注解是一个用来处理请求地址映射的注解,可用于映射一个请求或一个方法,可以用在类或方法上。
1.2.@RequestParam
@RequestParam主要用于将请求参数区域的数据映射到控制层方法的参数上
1.3.@RequestBody
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(即请求体中的数据的
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
1.4.@PathVariable
该注解请求URI中的模板变量部分到处理器功能处理方法的方法参数上的绑定。即当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。
先来在pom.xml配置文件中导入SLF4J的依赖
- "1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>ssmartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>ssm Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
-
-
-
- <spring.version>5.0.2.RELEASEspring.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <pagehelper.version>5.1.2pagehelper.version>
-
- <mybatis.spring.version>1.3.1mybatis.spring.version>
-
- <commons.dbcp2.version>2.1.1commons.dbcp2.version>
- <commons.pool2.version>2.4.3commons.pool2.version>
-
- <log4j2.version>2.9.1log4j2.version>
- <log4j2.disruptor.version>3.2.0log4j2.disruptor.version>
- <slf4j.version>1.7.13slf4j.version>
-
- <junit.version>4.12junit.version>
- <servlet.version>4.0.0servlet.version>
- <lombok.version>1.18.2lombok.version>
-
-
- <jstl.version>1.2jstl.version>
- <standard.version>1.1.2standard.version>
-
- <spring.version>5.0.2.RELEASEspring.version>
- <jackson.version>2.9.3jackson.version>
- properties>
-
-
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>${commons.dbcp2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>${commons.pool2.version}version>
- dependency>
-
-
-
-
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>${slf4j.version}version>
- dependency>
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>jcl-over-slf4jartifactId>
- <version>${slf4j.version}version>
- <scope>runtimescope>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-slf4j-implartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- <scope>runtimescope>
- dependency>
-
-
- <dependency>
- <groupId>com.lmaxgroupId>
- <artifactId>disruptorartifactId>
- <version>${log4j2.disruptor.version}version>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>${lombok.version}version>
- <scope>providedscope>
- dependency>
-
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>${jstl.version}version>
- dependency>
- <dependency>
- <groupId>taglibsgroupId>
- <artifactId>standardartifactId>
- <version>${standard.version}version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>${jackson.version}version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-coreartifactId>
- <version>${jackson.version}version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-annotationsartifactId>
- <version>${jackson.version}version>
- dependency>
- dependencies>
-
-
- <build>
- <finalName>ssmfinalName>
-
- <resources>
-
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
-
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>jdbc.propertiesinclude>
- <include>*.xmlinclude>
- includes>
- resource>
- resources>
-
-
- <plugins>
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>${maven.compiler.plugin.version}version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>${maven.compiler.plugin.version}version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- plugin>
-
- plugins>
- build>
- project>
- package com.sy.web;
-
- import com.sy.model.Book;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.Map;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-09-05 15:46
- */
- @Slf4j
- @Controller
- @RequestMapping("/param")
- public class ParamController {
- @RequestMapping("/hello1")
- public String index(String bname,Integer bid){
- //System.out.println("hello springmvc!!!");
- log.info("简单类型参数:bname:{},bid:{}",bname,bid);
- return "index";
- }
效果展示:
- @RequestMapping("/hello2")
- public String index2(Book book, HttpServletRequest request){
- //System.out.println("hello springmvc!!!");
- log.info("复杂类型参数:bname:{},bid:{}",request.getParameter("bname"),
- request.getParameter("bid"));
- log.info("复杂类型参数:book:{} " ,
- book.toString());
-
- return "index";
- }
效果展示:
- @RequestMapping("/hello3")
- public String index3(
- @RequestParam String bname,
- @RequestParam (required = false) Integer bid){
- //System.out.println("hello springmvc!!!");
- log.info("@RequestParam类型参数:bname:{},bid:{}",bname,bid);
- return "index";
- }
效果展示:
- @RequestMapping("/hello4/{bid}")
- public String index4(@PathVariable("bid") Integer bid){
- //System.out.println("hello springmvc!!!");
- log.info("@PathVariable类型参数:bid:{}",bid);
- return "index";
- }
效果展示:
- @RequestMapping("/hello5")
- public String index5(Map map){
- //System.out.println("hello springmvc!!!");
- log.info("RequestBody类型参数:map:{}",map);
- return "index";
- }
效果展示:
方需要借助一个测试软件传参数,我用的是Eolink
- // 查询请求
- @GetMapping
- public String type1(){
- System.out.println("GetMapping!!!");
- return "index";
- }
- //新增请求
- @PostMapping
- public String type2(){
- System.out.println("GetMapping!!!");
- return "index";
- }
- //修改请求
- @PutMapping
- public String type3(){
- System.out.println("PutMapping!!!");
- return "index";
- }
- //删除请求
- @DeleteMapping
- public String type4(){
- System.out.println("DeleteMapping!!!");
- return "index";
- }
创建一个ResponseUtil工具类,辅助完成测试代码如下 :
- package com.sy.utis;
-
- import com.fasterxml.jackson.databind.ObjectMapper;
-
- import javax.servlet.http.HttpServletResponse;
- import java.io.PrintWriter;
-
- public class ResponseUtil {
-
- public static void write(HttpServletResponse response,Object o)throws Exception{
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out=response.getWriter();
- out.println(o.toString());
- out.flush();
- out.close();
- }
-
- public static void writeJson(HttpServletResponse response,Object o)throws Exception{
- ObjectMapper om = new ObjectMapper();
- // om.writeValueAsString(o)代表了json串
- write(response, om.writeValueAsString(o));
- }
- }
创建一个ReturnController类,来进行方法的请求测试(包含关于返回值的所以方法)。
处理器对请求处理后,无需跳转到其它任何资源,此时可以让处理器方法返回 void。
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-09-05 19:25
- */
- @Controller
- @RequestMapping("/rs")
- public class RetrurnController {
- @RequestMapping("/hello1")
- public void hello1(HttpServletResponse response){
- Map <String, Object> map=new HashMap<>();
- map.put("code",200);
- map.put("mag","成功添加。。。");
- try {
- ResponseUtil.writeJson(response,map);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
测试结果:
- @RequestMapping("/hello3")
- public String hello3() {
- return "index";
- }
- @RequestMapping("/hello4")
- public String hello4(
- Model model,
- HttpServletRequest request) {
- model.addAttribute("name","米西米西");
- request.setAttribute("chapter","花不垃圾");
- return "index";
- }
测试结果:
转发和重定向的实现方式有所不同。转发是在服务器端进行处理,服务器接收到用户的请求后,将请求转发到另一个URL,并将响应返回给用户。重定向是通过发送特定的HTTP响应代码来告诉浏览器将用户的请求重定向到另一个URL,浏览器接收到重定向响应后,会自动发送新的请求到重定向的URL。
使用场景方面,转发适用于需要在服务器端进行一些处理后,将请求转发到其他页面或处理逻辑的情况。转发可以保持用户的URL不变,用户在浏览器中看到的URL仍然是原始的URL。重定向适用于需要将用户导航到其他页面或处理逻辑的情况,重定向会导致浏览器发送新的请求到重定向的URL,并在浏览器的地址栏中显示新的URL。 增删改都是用重定向