• SpringMVC之综合案例:参数传递,向页面传参,页面跳转


    • 参数传递
    • 向页面传参
    • 页面跳转

    1.参数传递

    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. org.example
    6. ssm
    7. 1.0-SNAPSHOT
    8. war
    9. ssm 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. org.apache.commons
    66. commons-pool2
    67. ${commons.pool2.version}
    68. org.slf4j
    69. slf4j-api
    70. ${slf4j.version}
    71. org.slf4j
    72. jcl-over-slf4j
    73. ${slf4j.version}
    74. runtime
    75. com.fasterxml.jackson.core
    76. jackson-databind
    77. ${jackson.version}
    78. com.fasterxml.jackson.core
    79. jackson-core
    80. ${jackson.version}
    81. com.fasterxml.jackson.core
    82. jackson-annotations
    83. ${jackson.version}
    84. org.apache.logging.log4j
    85. log4j-api
    86. ${log4j2.version}
    87. org.apache.logging.log4j
    88. log4j-core
    89. ${log4j2.version}
    90. org.apache.logging.log4j
    91. log4j-slf4j-impl
    92. ${log4j2.version}
    93. org.apache.logging.log4j
    94. log4j-web
    95. ${log4j2.version}
    96. runtime
    97. com.lmax
    98. disruptor
    99. ${log4j2.disruptor.version}
    100. junit
    101. junit
    102. ${junit.version}
    103. javax.servlet
    104. javax.servlet-api
    105. ${servlet.version}
    106. provided
    107. org.projectlombok
    108. lombok
    109. ${lombok.version}
    110. provided
    111. org.springframework
    112. spring-webmvc
    113. ${spring.version}
    114. jstl
    115. jstl
    116. ${jstl.version}
    117. taglibs
    118. standard
    119. ${standard.version}
    120. ssm
    121. src/main/java
    122. **/*.xml
    123. src/main/resources
    124. jdbc.properties
    125. *.xml
    126. org.apache.maven.plugins
    127. maven-compiler-plugin
    128. ${maven.compiler.plugin.version}
    129. ${maven.compiler.source}
    130. ${maven.compiler.target}
    131. ${project.build.sourceEncoding}
    132. org.mybatis.generator
    133. mybatis-generator-maven-plugin
    134. 1.3.2
    135. mysql
    136. mysql-connector-java
    137. ${mysql.version}
    138. true
    139. maven-clean-plugin
    140. 3.1.0
    141. maven-resources-plugin
    142. 3.0.2
    143. maven-compiler-plugin
    144. 3.8.0
    145. maven-surefire-plugin
    146. 2.22.1
    147. maven-war-plugin
    148. 3.2.2
    149. maven-install-plugin
    150. 2.5.2
    151. maven-deploy-plugin
    152. 2.8.2
    1. //index.xml
    2. <%--
    3. Created by IntelliJ IDEA.
    4. User: 朱
    5. Date: 2023/9/6
    6. Time: 12:40
    7. To change this template use File | Settings | File Templates.
    8. --%>
    9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    10. Title
    11. springmvc 雷猴

    1. package com.zlj.web;
    2. import com.zlj.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 zlj
    10. * @create 2023-09-06 12:32
    11. */
    12. @Slf4j
    13. @Controller
    14. @RequestMapping("/param")
    15. public class ParamController {
    16. @RequestMapping("/hello1")
    17. public String index(String bname,Integer bid){
    18. // System.out.println("hello springmvc...");
    19. log.info("简单类型参数:bname:{},bid:{}",bname,bid);
    20. // fail.. error waring info debug
    21. return "index";
    22. }
    23. @RequestMapping("/hello2")
    24. public String hello2(Book book, HttpServletRequest request){
    25. // System.out.println("hello springmvc...");
    26. // servlet参数获取方式
    27. log.info("复杂类型参数:bname:{},bid:{}",
    28. request.getParameter("bname"),
    29. request.getParameter("bid"));
    30. // 复杂传参
    31. log.info("复杂类型参数:book:{}",
    32. book.toString());
    33. // fail.. error waring info debug
    34. return "index";
    35. }
    36. @RequestMapping("/hello3")
    37. public String hello3(
    38. @RequestParam String bname,
    39. @RequestParam(required = false) Integer bid){
    40. // System.out.println("hello springmvc...");
    41. log.info("简单类型参数:bname:{},bid:{}",bname,bid);
    42. // fail.. error waring info debug
    43. return "index";
    44. }
    45. @RequestMapping("/hello4/{bid}")
    46. public String hello4(@PathVariable("bid") Integer bid){
    47. // System.out.println("hello springmvc...");
    48. log.info("@pathvariable:bid:{}",bid);
    49. // fail.. error waring info debug
    50. return "index";
    51. }
    52. @RequestMapping("/hello5")
    53. public String hello5(Map map){
    54. // System.out.println("hello springmvc...");
    55. log.info("@RequestBody:map:{}",map);
    56. // fail.. error waring info debug
    57. return "index";
    58. }
    59. @RequestMapping("/hello6")
    60. public String hello6(@RequestBody Map map){
    61. // System.out.println("hello springmvc...");
    62. log.info("@RequestBody:map:{}",map);
    63. // fail.. error waring info debug
    64. return "index";
    65. }
    66. @RequestMapping("/hello7")
    67. public String hello7(@RequestHeader("jwt") String jwt){
    68. // System.out.println("hello springmvc...");
    69. log.info("@RequestHeader:jwt:{}",jwt);
    70. // fail.. error waring info debug
    71. return "index";
    72. }
    73. @RequestMapping("/hello8")
    74. public String hello8(
    75. Book book,
    76. @RequestBody Map map,
    77. @RequestHeader("jwt") String jwt){
    78. // System.out.println("hello springmvc...");
    79. log.info("book:book:{}",book.toString());
    80. log.info("@RequestBorder:map:{}",map);
    81. log.info("@RequestHeader:jwt:{}",jwt);
    82. // fail.. error waring info debug
    83. return "index";
    84. }
    85. @GetMapping
    86. public String type1(@RequestBody Map map){
    87. System.out.println("getMapping...");
    88. return "index";
    89. }
    90. @PostMapping
    91. public String type2(@RequestBody Map map){
    92. System.out.println("postMapping...");
    93. return "index";
    94. }
    95. @PutMapping
    96. public String type3(@RequestBody Map map){
    97. System.out.println("putMapping...");
    98. return "index";
    99. }
    100. @DeleteMapping
    101. public String type4(@RequestBody Map map){
    102. System.out.println("DeleteMapping...");
    103. return "index";
    104. }
    105. //requestMapping=GetMapping+PostMapping+PutMapping+DeleteMapping
    106. // @RequestMapping不安全,不具备标识意义
    107. //@compontent=repository+service+controller
    108. }

    注:方法如以上,hell1到hello5通过浏览器打印到控制台输出;hello6,hello7,hello8以及type1到type4用Apikit软件发送到打印台。

    方法6中@requestBody需要导入依赖,上面pom.xml已经是完整版

     hello6的打印方法,及结果

     hello6的打印方法第二种写法

    hello7及hello8的打印结果,此处不详细,可以去搜索资料看怎么熟练并应用Apikit

    2.向页面传参

    1. package com.zlj.web;
    2. import com.zlj.util.ResponseUtil;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.ui.Model;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import org.springframework.web.servlet.ModelAndView;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.util.HashMap;
    11. import java.util.Map;
    12. /**
    13. * @author zlj
    14. * @create 2023-09-06 18:20
    15. */
    16. @Controller
    17. @RequestMapping("/rs")
    18. public class ReturnController {
    19. @RequestMapping("/hello1")
    20. public void hello1(HttpServletResponse response){
    21. Map map=new HashMap<>();
    22. map.put("code",200);
    23. map.put("msg","成功添加。。。");
    24. try {
    25. ResponseUtil.writeJson(response,map);
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. @ResponseBody //响应json数据
    31. @RequestMapping("/hello2")
    32. public Map hello2(HttpServletResponse response){
    33. Map map=new HashMap<>();
    34. map.put("code",200);
    35. map.put("msg","成功添加。。。");
    36. return map;
    37. }
    38. @RequestMapping("/hello3")
    39. public String hello3(){
    40. return "index";
    41. }
    42. //String+mondel
    43. @RequestMapping("/hello4")
    44. public String hello4(Model model, HttpServletRequest request){
    45. model.addAttribute("currenName","永州鸭");
    46. request.setAttribute("location","道州");
    47. return "index";
    48. }
    49. //modelandView
    50. @RequestMapping("/hello5")
    51. public ModelAndView hello5(){
    52. ModelAndView mv=new ModelAndView();
    53. mv.addObject("sign","肥而不腻");
    54. mv.setViewName("index");
    55. return mv;
    56. }
    57. }
    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: 朱
    4. Date: 2023/9/6
    5. Time: 12:40
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. Title
    10. springmvc 雷猴

    11. 用户名:${currenName}
    12. 原产地:${location}
    13. 评价:${sign}

    3.页面跳转(hello6,hello7,hello8,hello9)

    1. package com.zlj.web;
    2. import com.zlj.util.ResponseUtil;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.ui.Model;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import org.springframework.web.servlet.ModelAndView;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.util.HashMap;
    11. import java.util.Map;
    12. /**
    13. * @author zlj
    14. * @create 2023-09-06 18:20
    15. */
    16. @Controller
    17. @RequestMapping("/rs")
    18. public class ReturnController {
    19. @RequestMapping("/hello1")
    20. public void hello1(HttpServletResponse response) {
    21. Map map = new HashMap<>();
    22. map.put("code", 200);
    23. map.put("msg", "成功添加。。。");
    24. try {
    25. ResponseUtil.writeJson(response, map);
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. @ResponseBody //响应json数据
    31. @RequestMapping("/hello2")
    32. public Map hello2(HttpServletResponse response) {
    33. Map map = new HashMap<>();
    34. map.put("code", 200);
    35. map.put("msg", "成功添加。。。");
    36. return map;
    37. }
    38. @RequestMapping("/hello3")
    39. public String hello3() {
    40. return "index";
    41. }
    42. //String+mondel
    43. @RequestMapping("/hello4")
    44. public String hello4(Model model, HttpServletRequest request) {
    45. model.addAttribute("currenName", "永州鸭");
    46. request.setAttribute("location", "道州");
    47. return "index";
    48. }
    49. //modelandView
    50. @RequestMapping("/hello5")
    51. public ModelAndView hello5() {
    52. ModelAndView mv = new ModelAndView();
    53. mv.addObject("sign", "肥而不腻");
    54. mv.setViewName("index");
    55. return mv;
    56. }
    57. // 场景一:转发到后台的某一个方法(当前类)
    58. @RequestMapping("/hello6")
    59. public String hello6() {
    60. System.out.println("helo6...");
    61. return "forward:hello2";
    62. }
    63. // 场景二:转发到后台的某一个方法(其他类)
    64. @RequestMapping("/hello7")
    65. public String hello7() {
    66. System.out.println("hello7...");
    67. return "forward:/param/hello1";
    68. }
    69. // 场景三:重定向到后台的某一个方法(当前类)
    70. @RequestMapping("/hello8")
    71. public String hello8() {
    72. System.out.println("helo8...");
    73. return "redirect:hello2";
    74. }
    75. // 场景四:重定向到后台的某一个方法(其他类)
    76. @RequestMapping("/hello9")
    77. public String hello9() {
    78. System.out.println("hello9...");
    79. return "redirect:/param/hello1";
    80. }
    81. }

  • 相关阅读:
    搞一个自己用的node-cli
    红米note5 拆金属外壳
    WPF在win10/11上启用模糊特效 适配Dark/Light Mode
    HTML5 Canvas 限定文本区域大小,文字自动换行,自动缩放
    Servlet——进阶
    Day17-购物车页面-结算-动态计算已勾选商品的数据和选中状态
    2022最全Java后端面试真题、两万字1000+道堪称史上最强的面试题不接受任何反驳
    SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
    vue项目,PDA嵌入安卓,实现app热更新
    Redis核心数据结构【list】【从入门到入坟】
  • 原文地址:https://blog.csdn.net/weixin_73471776/article/details/132720542