• springMVC基础技术使用


    目录

    1.常用注解 

       1.1@RequestMapping

       1.2.@RequestParam

          1.3.@RequestBody

          1.4.@PathVariable

    2.参数传递

    2.1 slf4j-----日志

    2.2基础类型

    2.3复杂类型

    2.4@RequestParam

     2.5@PathVariable

    2.6@RequestBody

     2.7请求方法(增删改查)

    3.返回值

    3.1void 返回值 

    3.2String 返回值

    4.3 model+String

    4.页面跳转

    4.1转发

    4.2重定向


    1.常用注解 

       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注解绑定它传过来的值到方法的参数上。

    2.参数传递

    2.1 slf4j-----日志

    SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。

    先来在pom.xml配置文件中导入SLF4J的依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>ssmartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>ssm Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
    16. <spring.version>5.0.2.RELEASEspring.version>
    17. <mybatis.version>3.4.5mybatis.version>
    18. <mysql.version>5.1.44mysql.version>
    19. <pagehelper.version>5.1.2pagehelper.version>
    20. <mybatis.spring.version>1.3.1mybatis.spring.version>
    21. <commons.dbcp2.version>2.1.1commons.dbcp2.version>
    22. <commons.pool2.version>2.4.3commons.pool2.version>
    23. <log4j2.version>2.9.1log4j2.version>
    24. <log4j2.disruptor.version>3.2.0log4j2.disruptor.version>
    25. <slf4j.version>1.7.13slf4j.version>
    26. <junit.version>4.12junit.version>
    27. <servlet.version>4.0.0servlet.version>
    28. <lombok.version>1.18.2lombok.version>
    29. <jstl.version>1.2jstl.version>
    30. <standard.version>1.1.2standard.version>
    31. <spring.version>5.0.2.RELEASEspring.version>
    32. <jackson.version>2.9.3jackson.version>
    33. properties>
    34. <dependencies>
    35. <dependency>
    36. <groupId>org.springframeworkgroupId>
    37. <artifactId>spring-contextartifactId>
    38. <version>${spring.version}version>
    39. dependency>
    40. <dependency>
    41. <groupId>org.springframeworkgroupId>
    42. <artifactId>spring-ormartifactId>
    43. <version>${spring.version}version>
    44. dependency>
    45. <dependency>
    46. <groupId>org.springframeworkgroupId>
    47. <artifactId>spring-txartifactId>
    48. <version>${spring.version}version>
    49. dependency>
    50. <dependency>
    51. <groupId>org.springframeworkgroupId>
    52. <artifactId>spring-aspectsartifactId>
    53. <version>${spring.version}version>
    54. dependency>
    55. <dependency>
    56. <groupId>org.springframeworkgroupId>
    57. <artifactId>spring-webartifactId>
    58. <version>${spring.version}version>
    59. dependency>
    60. <dependency>
    61. <groupId>org.springframeworkgroupId>
    62. <artifactId>spring-testartifactId>
    63. <version>${spring.version}version>
    64. dependency>
    65. <dependency>
    66. <groupId>org.mybatisgroupId>
    67. <artifactId>mybatisartifactId>
    68. <version>${mybatis.version}version>
    69. dependency>
    70. <dependency>
    71. <groupId>mysqlgroupId>
    72. <artifactId>mysql-connector-javaartifactId>
    73. <version>${mysql.version}version>
    74. dependency>
    75. <dependency>
    76. <groupId>com.github.pagehelpergroupId>
    77. <artifactId>pagehelperartifactId>
    78. <version>${pagehelper.version}version>
    79. dependency>
    80. <dependency>
    81. <groupId>org.mybatisgroupId>
    82. <artifactId>mybatis-springartifactId>
    83. <version>${mybatis.spring.version}version>
    84. dependency>
    85. <dependency>
    86. <groupId>org.apache.commonsgroupId>
    87. <artifactId>commons-dbcp2artifactId>
    88. <version>${commons.dbcp2.version}version>
    89. dependency>
    90. <dependency>
    91. <groupId>org.apache.commonsgroupId>
    92. <artifactId>commons-pool2artifactId>
    93. <version>${commons.pool2.version}version>
    94. dependency>
    95. <dependency>
    96. <groupId>org.slf4jgroupId>
    97. <artifactId>slf4j-apiartifactId>
    98. <version>${slf4j.version}version>
    99. dependency>
    100. <dependency>
    101. <groupId>org.slf4jgroupId>
    102. <artifactId>jcl-over-slf4jartifactId>
    103. <version>${slf4j.version}version>
    104. <scope>runtimescope>
    105. dependency>
    106. <dependency>
    107. <groupId>org.apache.logging.log4jgroupId>
    108. <artifactId>log4j-apiartifactId>
    109. <version>${log4j2.version}version>
    110. dependency>
    111. <dependency>
    112. <groupId>org.apache.logging.log4jgroupId>
    113. <artifactId>log4j-coreartifactId>
    114. <version>${log4j2.version}version>
    115. dependency>
    116. <dependency>
    117. <groupId>org.apache.logging.log4jgroupId>
    118. <artifactId>log4j-slf4j-implartifactId>
    119. <version>${log4j2.version}version>
    120. dependency>
    121. <dependency>
    122. <groupId>org.apache.logging.log4jgroupId>
    123. <artifactId>log4j-webartifactId>
    124. <version>${log4j2.version}version>
    125. <scope>runtimescope>
    126. dependency>
    127. <dependency>
    128. <groupId>com.lmaxgroupId>
    129. <artifactId>disruptorartifactId>
    130. <version>${log4j2.disruptor.version}version>
    131. dependency>
    132. <dependency>
    133. <groupId>junitgroupId>
    134. <artifactId>junitartifactId>
    135. <version>${junit.version}version>
    136. dependency>
    137. <dependency>
    138. <groupId>javax.servletgroupId>
    139. <artifactId>javax.servlet-apiartifactId>
    140. <version>${servlet.version}version>
    141. <scope>providedscope>
    142. dependency>
    143. <dependency>
    144. <groupId>org.projectlombokgroupId>
    145. <artifactId>lombokartifactId>
    146. <version>${lombok.version}version>
    147. <scope>providedscope>
    148. dependency>
    149. <dependency>
    150. <groupId>org.springframeworkgroupId>
    151. <artifactId>spring-webmvcartifactId>
    152. <version>${spring.version}version>
    153. dependency>
    154. <dependency>
    155. <groupId>jstlgroupId>
    156. <artifactId>jstlartifactId>
    157. <version>${jstl.version}version>
    158. dependency>
    159. <dependency>
    160. <groupId>taglibsgroupId>
    161. <artifactId>standardartifactId>
    162. <version>${standard.version}version>
    163. dependency>
    164. <dependency>
    165. <groupId>com.fasterxml.jackson.coregroupId>
    166. <artifactId>jackson-databindartifactId>
    167. <version>${jackson.version}version>
    168. dependency>
    169. <dependency>
    170. <groupId>com.fasterxml.jackson.coregroupId>
    171. <artifactId>jackson-coreartifactId>
    172. <version>${jackson.version}version>
    173. dependency>
    174. <dependency>
    175. <groupId>com.fasterxml.jackson.coregroupId>
    176. <artifactId>jackson-annotationsartifactId>
    177. <version>${jackson.version}version>
    178. dependency>
    179. dependencies>
    180. <build>
    181. <finalName>ssmfinalName>
    182. <resources>
    183. <resource>
    184. <directory>src/main/javadirectory>
    185. <includes>
    186. <include>**/*.xmlinclude>
    187. includes>
    188. resource>
    189. <resource>
    190. <directory>src/main/resourcesdirectory>
    191. <includes>
    192. <include>jdbc.propertiesinclude>
    193. <include>*.xmlinclude>
    194. includes>
    195. resource>
    196. resources>
    197. <plugins>
    198. <plugin>
    199. <artifactId>maven-clean-pluginartifactId>
    200. <version>3.1.0version>
    201. plugin>
    202. <plugin>
    203. <artifactId>maven-resources-pluginartifactId>
    204. <version>3.0.2version>
    205. plugin>
    206. <plugin>
    207. <artifactId>maven-compiler-pluginartifactId>
    208. <version>3.8.0version>
    209. plugin>
    210. <plugin>
    211. <artifactId>maven-surefire-pluginartifactId>
    212. <version>2.22.1version>
    213. plugin>
    214. <plugin>
    215. <artifactId>maven-war-pluginartifactId>
    216. <version>3.2.2version>
    217. plugin>
    218. <plugin>
    219. <artifactId>maven-install-pluginartifactId>
    220. <version>2.5.2version>
    221. plugin>
    222. <plugin>
    223. <artifactId>maven-deploy-pluginartifactId>
    224. <version>2.8.2version>
    225. plugin>
    226. <plugin>
    227. <groupId>org.apache.maven.pluginsgroupId>
    228. <artifactId>maven-compiler-pluginartifactId>
    229. <version>${maven.compiler.plugin.version}version>
    230. <configuration>
    231. <source>${maven.compiler.source}source>
    232. <target>${maven.compiler.target}target>
    233. <encoding>${project.build.sourceEncoding}encoding>
    234. configuration>
    235. plugin>
    236. <plugin>
    237. <groupId>org.apache.maven.pluginsgroupId>
    238. <artifactId>maven-compiler-pluginartifactId>
    239. <version>${maven.compiler.plugin.version}version>
    240. <configuration>
    241. <source>${maven.compiler.source}source>
    242. <target>${maven.compiler.target}target>
    243. <encoding>${project.build.sourceEncoding}encoding>
    244. configuration>
    245. plugin>
    246. <plugin>
    247. <groupId>org.mybatis.generatorgroupId>
    248. <artifactId>mybatis-generator-maven-pluginartifactId>
    249. <version>1.3.2version>
    250. <dependencies>
    251. <dependency>
    252. <groupId>mysqlgroupId>
    253. <artifactId>mysql-connector-javaartifactId>
    254. <version>${mysql.version}version>
    255. dependency>
    256. dependencies>
    257. <configuration>
    258. <overwrite>trueoverwrite>
    259. configuration>
    260. plugin>
    261. plugins>
    262. build>
    263. project>

    2.2基础类型

    1. package com.sy.web;
    2. import com.sy.model.Book;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.*;
    6. import javax.servlet.http.HttpServletRequest;
    7. import java.util.Map;
    8. /**
    9. * @author 谌艳
    10. * @site www.shenyan.com
    11. * @create 2023-09-05 15:46
    12. */
    13. @Slf4j
    14. @Controller
    15. @RequestMapping("/param")
    16. public class ParamController {
    17. @RequestMapping("/hello1")
    18. public String index(String bname,Integer bid){
    19. //System.out.println("hello springmvc!!!");
    20. log.info("简单类型参数:bname:{},bid:{}",bname,bid);
    21. return "index";
    22. }

     效果展示:

    2.3复杂类型

    1. @RequestMapping("/hello2")
    2. public String index2(Book book, HttpServletRequest request){
    3. //System.out.println("hello springmvc!!!");
    4. log.info("复杂类型参数:bname:{},bid:{}",request.getParameter("bname"),
    5. request.getParameter("bid"));
    6. log.info("复杂类型参数:book:{} " ,
    7. book.toString());
    8. return "index";
    9. }

     效果展示:

    2.4@RequestParam

    1. @RequestMapping("/hello3")
    2. public String index3(
    3. @RequestParam String bname,
    4. @RequestParam (required = false) Integer bid){
    5. //System.out.println("hello springmvc!!!");
    6. log.info("@RequestParam类型参数:bname:{},bid:{}",bname,bid);
    7. return "index";
    8. }

      效果展示:

     2.5@PathVariable

    1. @RequestMapping("/hello4/{bid}")
    2. public String index4(@PathVariable("bid") Integer bid){
    3. //System.out.println("hello springmvc!!!");
    4. log.info("@PathVariable类型参数:bid:{}",bid);
    5. return "index";
    6. }

     效果展示:

    2.6@RequestBody

    1. @RequestMapping("/hello5")
    2. public String index5(Map map){
    3. //System.out.println("hello springmvc!!!");
    4. log.info("RequestBody类型参数:map:{}",map);
    5. return "index";
    6. }

     效果展示:

    方需要借助一个测试软件传参数,我用的是Eolink

     2.7请求方法(增删改查)

    1. // 查询请求
    2. @GetMapping
    3. public String type1(){
    4. System.out.println("GetMapping!!!");
    5. return "index";
    6. }
    7. //新增请求
    8. @PostMapping
    9. public String type2(){
    10. System.out.println("GetMapping!!!");
    11. return "index";
    12. }
    13. //修改请求
    14. @PutMapping
    15. public String type3(){
    16. System.out.println("PutMapping!!!");
    17. return "index";
    18. }
    19. //删除请求
    20. @DeleteMapping
    21. public String type4(){
    22. System.out.println("DeleteMapping!!!");
    23. return "index";
    24. }


    3.返回值

    创建一个ResponseUtil工具类,辅助完成测试代码如下 : 

    1. package com.sy.utis;
    2. import com.fasterxml.jackson.databind.ObjectMapper;
    3. import javax.servlet.http.HttpServletResponse;
    4. import java.io.PrintWriter;
    5. public class ResponseUtil {
    6. public static void write(HttpServletResponse response,Object o)throws Exception{
    7. response.setContentType("text/html;charset=utf-8");
    8. PrintWriter out=response.getWriter();
    9. out.println(o.toString());
    10. out.flush();
    11. out.close();
    12. }
    13. public static void writeJson(HttpServletResponse response,Object o)throws Exception{
    14. ObjectMapper om = new ObjectMapper();
    15. // om.writeValueAsString(o)代表了json串
    16. write(response, om.writeValueAsString(o));
    17. }
    18. }

    创建一个ReturnController类,来进行方法的请求测试(包含关于返回值的所以方法)。

    3.1void 返回值 

            处理器对请求处理后,无需跳转到其它任何资源,此时可以让处理器方法返回 void。

    1. /**
    2. * @author 谌艳
    3. * @site www.shenyan.com
    4. * @create 2023-09-05 19:25
    5. */
    6. @Controller
    7. @RequestMapping("/rs")
    8. public class RetrurnController {
    9. @RequestMapping("/hello1")
    10. public void hello1(HttpServletResponse response){
    11. Map <String, Object> map=new HashMap<>();
    12. map.put("code",200);
    13. map.put("mag","成功添加。。。");
    14. try {
    15. ResponseUtil.writeJson(response,map);
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. }

    测试结果:

    3.2String 返回值

    1. @RequestMapping("/hello3")
    2. public String hello3() {
    3. return "index";
    4. }

    4.3 model+String

    1. @RequestMapping("/hello4")
    2. public String hello4(
    3. Model model,
    4. HttpServletRequest request) {
    5. model.addAttribute("name","米西米西");
    6. request.setAttribute("chapter","花不垃圾");
    7. return "index";
    8. }

    测试结果:

    4.页面跳转

    4.1转发

    4.2重定向

    转发重定向的实现方式有所不同。转发是在服务器端进行处理,服务器接收到用户的请求后,将请求转发到另一个URL,并将响应返回给用户。重定向是通过发送特定的HTTP响应代码来告诉浏览器将用户的请求重定向到另一个URL,浏览器接收到重定向响应后,会自动发送新的请求到重定向的URL。

            使用场景方面,转发适用于需要在服务器端进行一些处理后,将请求转发到其他页面或处理逻辑的情况。转发可以保持用户的URL不变,用户在浏览器中看到的URL仍然是原始的URL。重定向适用于需要将用户导航到其他页面或处理逻辑的情况,重定向会导致浏览器发送新的请求到重定向的URL,并在浏览器的地址栏中显示新的URL。 增删改都是用重定向

  • 相关阅读:
    【numpy简介、入门、数组创建】
    架构师之路七分布式系统演化之duboo详解
    配置XP虚拟机和Win 10宿主机互相ping通
    uni-app 框架中使用 vuex(store)
    中文版FL Studio 20 for Mac破解版水果编曲软件 V20.8.3(1574)支持M1/M2+Intel
    统一潮流控制器 (UPFC) 的应用,以增强电力系统中的电压稳定性(Matlab代码实现)
    Kafka安装启动(含安装包)
    解决:使用ss-calendar组件(Dcloud插件)报错 “TypeError: date.split is not a function“
    webpack 使用 二
    每日一题-轮转数组
  • 原文地址:https://blog.csdn.net/2301_76988707/article/details/132713060