• SpringMvc--综合案例


    目录

    1.SpringMvc的常用注解

    2.参数传递

    基础类型(String)

    创建一个paramController类: 

    创建一个index.jsp

     测试结果

    复杂方式

    ​编辑 测试结果

    @RequestParam

    测试结果 

    @PathVariable

    测试结果 

     @RequestBody

    pom.xml依赖导入

    输出结果 

     @RequestHeader

     测试结果

    3.返回值

    void

    测试结果

    Map

     测试结果

    ​编辑

    String

     测试结果

    String+model

     测试结果

    ModelAndView

    4.页面跳转

    转发

    测试结果

    ​编辑 

    重定向

    测试结果


    1.SpringMvc的常用注解

    1. @Controller:用于声明一个控制器类。

    2. @RequestMapping:用于映射请求 URL 到控制器类或处理方法上。

    3. @RequestParam:用于获取请求参数的值。

    4. @PathVariable:用于获取 URL 中的参数值。

    5. @ResponseBody:用于将返回值转化为 JSON 格式或其他格式的数据。

    6. @ModelAttribute:用于将请求参数绑定到模型对象上。

    7. @Valid:用于验证请求参数。

    8. @InitBinder:用于定义数据类型转换和格式化规则。

    9. @SessionAttributes:用于将模型对象存储到会话中。

    10. @ExceptionHandler:用于处理控制器方法中抛出的异常。

    11. @RequestMapping(value=“”, method=RequestMethod.GET) 用于处理 GET 请求。

    12. @RequestMapping(value=“”, method=RequestMethod.POST) 用于处理 POST 请求。

    13. @RequestMapping(value=“”, method=RequestMethod.PUT) 用于处理 PUT 请求。

    14. @RequestMapping(value=“”, method=RequestMethod.DELETE) 用于处理 DELETE 请求。

    2.参数传递

    基础类型(String)
    1. @RequestMapping("/hello1")
    2. public String toHello1(Integer bid,String bname){
    3. log.info(">>>> 基础类型+String传参:{},{}",bid,bname);
    4. return "index";
    5. }
    创建一个paramController类: 
    1. package com.zking.web;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. /**
    6. * @author bin人
    7. * @site www.zking.com
    8. * @company xy集团
    9. * @create 2023-09-05-14:44
    10. */
    11. @Slf4j
    12. @Controller
    13. @RequestMapping("/param")
    14. public class ParamController {
    15. @RequestMapping("/hello1")
    16. public String index(String bname,Integer bid){
    17. // System.out.println("放下屠刀");
    18. log.info("简单类型参数:bname:{},bid:{}",bname,bid);
    19. return "index";
    20. }
    21. }
    创建一个index.jsp
    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. Title
    3. 立地成佛

     测试结果

     

    复杂方式
     测试结果

     

    @RequestParam

    (用了这个required=false必须要传参数,要不然进入不了方法)

    测试结果 

    必须要传bname要不然会报错,也接受不到值

    传了值之后就可以正常访问了 

     

    @PathVariable
    1. @RequestMapping("/hello4/{bid}")
    2. public String hello4(@PathVariable("bid") Integer bid){
    3. log.info("@PathVariable:bid:{}",bid);
    4. // fail..error warning info debug
    5. return "index";
    6. }
    测试结果 

     @RequestBody
    1. @RequestMapping("/hello5/{bid}")
    2. public String hello5(Map map){
    3. log.info("@RequestBody:map:{}",map);
    4. // fail..error warning info debug
    5. return "index";
    6. }
    7. @RequestMapping("/hello6/{bid}")
    8. public String hello6(@RequestBody Map map){
    9. log.info("@RequestBody:map:{}",map);
    10. // fail..error warning info debug
    11. return "index";
    12. }
    想要用这个注解就必须导入架包依赖
    pom.xml依赖导入
    1. "1.0" encoding="UTF-8"?>
    2. "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. 4.0.0
    5. com.zking
    6. yxssm
    7. 1.0-SNAPSHOT
    8. war
    9. yxssm Maven Webapp
    10. http://www.example.com
    11. UTF-8
    12. 1.8
    13. 1.8
    14. 3.7.0
    15. 5.0.2.RELEASE
    16. 3.4.5
    17. 5.1.44
    18. 5.1.2
    19. 1.3.1
    20. 2.1.1
    21. 2.4.3
    22. 2.9.1
    23. 3.2.0
    24. 1.7.13
    25. 4.12
    26. 4.0.0
    27. 1.18.2
    28. 1.2
    29. 1.1.2
    30. 5.0.2.RELEASE
    31. 2.9.3
    32. org.springframework
    33. spring-context
    34. ${spring.version}
    35. org.springframework
    36. spring-orm
    37. ${spring.version}
    38. org.springframework
    39. spring-tx
    40. ${spring.version}
    41. org.springframework
    42. spring-aspects
    43. ${spring.version}
    44. org.springframework
    45. spring-web
    46. ${spring.version}
    47. org.springframework
    48. spring-test
    49. ${spring.version}
    50. org.mybatis
    51. mybatis
    52. ${mybatis.version}
    53. mysql
    54. mysql-connector-java
    55. ${mysql.version}
    56. com.github.pagehelper
    57. pagehelper
    58. ${pagehelper.version}
    59. org.mybatis
    60. mybatis-spring
    61. ${mybatis.spring.version}
    62. org.apache.commons
    63. commons-dbcp2
    64. ${commons.dbcp2.version}
    65. commons-pool2
    66. org.apache.commons
    67. org.apache.commons
    68. commons-pool2
    69. ${commons.pool2.version}
    70. org.slf4j
    71. slf4j-api
    72. ${slf4j.version}
    73. org.slf4j
    74. jcl-over-slf4j
    75. ${slf4j.version}
    76. runtime
    77. org.apache.logging.log4j
    78. log4j-api
    79. ${log4j2.version}
    80. org.apache.logging.log4j
    81. log4j-core
    82. ${log4j2.version}
    83. org.apache.logging.log4j
    84. log4j-slf4j-impl
    85. ${log4j2.version}
    86. slf4j-api
    87. org.slf4j
    88. org.apache.logging.log4j
    89. log4j-web
    90. ${log4j2.version}
    91. runtime
    92. com.lmax
    93. disruptor
    94. ${log4j2.disruptor.version}
    95. junit
    96. junit
    97. ${junit.version}
    98. javax.servlet
    99. javax.servlet-api
    100. ${servlet.version}
    101. provided
    102. org.projectlombok
    103. lombok
    104. ${lombok.version}
    105. provided
    106. org.springframework
    107. spring-webmvc
    108. ${spring.version}
    109. jstl
    110. jstl
    111. ${jstl.version}
    112. taglibs
    113. standard
    114. ${standard.version}
    115. com.fasterxml.jackson.core
    116. jackson-databind
    117. ${jackson.version}
    118. com.fasterxml.jackson.core
    119. jackson-core
    120. ${jackson.version}
    121. com.fasterxml.jackson.core
    122. jackson-annotations
    123. ${jackson.version}
    124. yxssm
    125. src/main/java
    126. **/*.xml
    127. src/main/resources
    128. jdbc.properties
    129. *.xml
    130. org.apache.maven.plugins
    131. maven-compiler-plugin
    132. ${maven.compiler.plugin.version}
    133. ${maven.compiler.source}
    134. ${maven.compiler.target}
    135. ${project.build.sourceEncoding}
    136. org.mybatis.generator
    137. mybatis-generator-maven-plugin
    138. 1.3.2
    139. mysql
    140. mysql-connector-java
    141. ${mysql.version}
    142. true
    143. maven-clean-plugin
    144. 3.1.0
    145. maven-resources-plugin
    146. 3.0.2
    147. maven-compiler-plugin
    148. 3.8.0
    149. maven-surefire-plugin
    150. 2.22.1
    151. maven-war-plugin
    152. 3.2.2
    153. maven-install-plugin
    154. 2.5.2
    155. maven-deploy-plugin
    156. 2.8.2

    在导入依赖后我们还需要postman或者apipost/eolink等工具发送请求数据。 因为浏览器发送不了JSON数据请求。所以我们需要安装Eolink等第三方工具进行测试。

     

    安装好后,按照以下步骤把参数填写好,query参数是往域名链接添加属性,如下: 

    输出结果 

    请看录像

    通过方法五跟方法六的对比,可得出@RequestBody适用于专门接受Json

     @RequestHeader
    1. @RequestMapping("/hello7")
    2. public String hello7(@RequestHeader("jwt") String jwt){
    3. // System.out.println("刘三金去拿奶茶喽。。。");
    4. log.info("@RequestHeader参数:jwt:{}",jwt);
    5. // fail..error warning info debug
    6. return "index";
    7. }
    8. @RequestMapping("/hello8")
    9. public String hello8(Book book,
    10. @RequestBody Map map,
    11. @RequestHeader("jwt") String jwt){
    12. // System.out.println("刘三金去拿奶茶喽。。。");
    13. log.info("Book:Book:{}",book.toString());
    14. log.info("@RequestBody参数:Map:{}",map);
    15. log.info("@RequestHeader参数:jwt:{}",jwt);
    16. // fail..error warning info debug
    17. return "index";
    18. }
     测试结果

    3.返回值

    void

    先写工具类:

    1. package com.zking.utils;
    2. import java.io.PrintWriter;
    3. import javax.servlet.http.HttpServletResponse;
    4. import com.fasterxml.jackson.databind.ObjectMapper;
    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. }

     

    1. package com.zking.web;
    2. import com.zking.utils.ResponseUtil;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.util.HashMap;
    7. import java.util.Map;
    8. /**
    9. * @author bing人
    10. * @site
    11. * @company xy集团
    12. * @create 2023-09-06 22:12
    13. */
    14. @Controller
    15. @RequestMapping("/rs")
    16. public class ReturnController {
    17. @RequestMapping("/hello1")
    18. public void hello1(HttpServletResponse response){
    19. Map map=new HashMap<>();
    20. map.put("code",200);
    21. map.put("msg","成功添加。");
    22. try {
    23. ResponseUtil.writeJson(response,map);
    24. } catch (Exception e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }
    测试结果

    Map
    1. @ResponseBody//响应json数据
    2. @RequestMapping("/hello2")
    3. public Map hello2(HttpServletResponse response) {
    4. Map map = new HashMap<>();
    5. map.put("code", 200);
    6. map.put("msg", "成功添加。");
    7. return map;
    8. }
     测试结果
    String
    1. @RequestMapping("/hello3")
    2. public String hello3() {
    3. return "index";
    4. }
     测试结果

    String+model
    1. //String+model
    2. @RequestMapping("/hello4")
    3. public String hello4(Model model, HttpServletRequest request) {
    4. model.addAttribute("currentName","浏阳鞭炮");
    5. request.setAttribute("location","来自于刘老板的");
    6. return "index";
    7. }
     测试结果

    ModelAndView

    4.页面跳转

    转发
    1. // 场景一:转发到后台的某一个方法(当前类)
    2. @RequestMapping("/hello6")
    3. public String hello6(){
    4. System.out.println("hello6");
    5. return "forward:hello2";
    6. }
    7. // 场景二:转发到后台的某一个方法(其他类)
    8. @RequestMapping("/hello7")
    9. public String hello7(){
    10. System.out.println("hello7");
    11. return "forward:/param/hello1";
    12. }
    测试结果
     

     

    重定向
    1. // 场景三:重定向到后台的某一个方法(当前类)
    2. @RequestMapping("/hello8")
    3. public String hello8(){
    4. System.out.println("hello8");
    5. return "forward:hello2";
    6. }
    7. // 场景四:重定向后台的某一个方法(其他类)
    8. @RequestMapping("/hello9")
    9. public String hello9(){
    10. System.out.println("hello9");
    11. return "forward:/param/hello1";
    12. }
    测试结果

     

  • 相关阅读:
    5 个开源的 Rust Web 开发框架,你选择哪个?
    ruby 版本管理RVM (ruby version manager)
    《CTF攻防世界web题》之我什么都不会(1)
    ubuntu:在ubuntu系统上使用apache2服务器部署多个vue项目
    vscode 如何断点调试ros1工程
    Python和Java代码实现:切线法求解一维最优化问题
    MIMO信道的随机性
    Fastjsonfan反序列化(1)
    MySQL事务基本操作(方式2)
    NTFS及文件共享
  • 原文地址:https://blog.csdn.net/2201_75455485/article/details/132722648