- 参数传递
- 向页面传参
- 页面跳转
1.参数传递
<?xml version="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.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<!--添加jar包依赖-->
<!--1.spring 5.0.2.RELEASE相关-->
<spring.version>5.0.2.RELEASE</spring.version>
<!--2.mybatis相关-->
<mybatis.version>3.4.5</mybatis.version>
<!--mysql-->
<mysql.version>5.1.44</mysql.version>
<!--pagehelper分页jar依赖-->
<pagehelper.version>5.1.2</pagehelper.version>
<!--mybatis与spring集成jar依赖-->
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<!--3.dbcp2连接池相关 druid-->
<commons.dbcp2.version>2.1.1</commons.dbcp2.version>
<commons.pool2.version>2.4.3</commons.pool2.version>
<!--4.log日志相关-->
<log4j2.version>2.9.1</log4j2.version>
<log4j2.disruptor.version>3.2.0</log4j2.disruptor.version>
<slf4j.version>1.7.13</slf4j.version>
<!--5.其他-->
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<lombok.version>1.18.2</lombok.version>
<!-- jstl+standard -->
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version>
<!-- spring -->
<spring.version>5.0.2.RELEASE</spring.version>
<jackson.version>2.9.3</jackson.version>
</properties>
<dependencies>
<!--1.spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--2.mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--pagehelper分页插件jar包依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!--mybatis与spring集成jar包依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!--3.dbcp2连接池相关-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${commons.dbcp2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${commons.pool2.version}</version>
</dependency>
<!-- log4j2日志相关依赖 -->
<!-- log配置:Log4j2 + Slf4j -->
<!-- slf4j核心包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!--核心log4j2jar包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--用于与slf4j保持桥接-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--web工程需要包含log4j-web,非web工程不需要-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2.version}</version>
<scope>runtime</scope>
</dependency>
<!--需要使用log4j2的AsyncLogger需要包含disruptor-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${log4j2.disruptor.version}</version>
</dependency>
<!--5.其他-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- spring mvc相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
<resources>
<!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>jdbc.properties</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<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.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</build>
</project>
//index.xml
<%--
Created by IntelliJ IDEA.
User: 朱
Date: 2023/9/6
Time: 12:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>springmvc 雷猴</h1>
</body>
</html>
package com.zlj.web;
import com.zlj.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 zlj
* @create 2023-09-06 12:32
*/
@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);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello2")
public String hello2(Book book, HttpServletRequest request){
// System.out.println("hello springmvc...");
// servlet参数获取方式
log.info("复杂类型参数:bname:{},bid:{}",
request.getParameter("bname"),
request.getParameter("bid"));
// 复杂传参
log.info("复杂类型参数:book:{}",
book.toString());
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello3")
public String hello3(
@RequestParam String bname,
@RequestParam(required = false) Integer bid){
// System.out.println("hello springmvc...");
log.info("简单类型参数:bname:{},bid:{}",bname,bid);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello4/{bid}")
public String hello4(@PathVariable("bid") Integer bid){
// System.out.println("hello springmvc...");
log.info("@pathvariable:bid:{}",bid);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello5")
public String hello5(Map map){
// System.out.println("hello springmvc...");
log.info("@RequestBody:map:{}",map);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello6")
public String hello6(@RequestBody Map map){
// System.out.println("hello springmvc...");
log.info("@RequestBody:map:{}",map);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello7")
public String hello7(@RequestHeader("jwt") String jwt){
// System.out.println("hello springmvc...");
log.info("@RequestHeader:jwt:{}",jwt);
// fail.. error waring info debug
return "index";
}
@RequestMapping("/hello8")
public String hello8(
Book book,
@RequestBody Map map,
@RequestHeader("jwt") String jwt){
// System.out.println("hello springmvc...");
log.info("book:book:{}",book.toString());
log.info("@RequestBorder:map:{}",map);
log.info("@RequestHeader:jwt:{}",jwt);
// fail.. error waring info debug
return "index";
}
@GetMapping
public String type1(@RequestBody Map map){
System.out.println("getMapping...");
return "index";
}
@PostMapping
public String type2(@RequestBody Map map){
System.out.println("postMapping...");
return "index";
}
@PutMapping
public String type3(@RequestBody Map map){
System.out.println("putMapping...");
return "index";
}
@DeleteMapping
public String type4(@RequestBody Map map){
System.out.println("DeleteMapping...");
return "index";
}
//requestMapping=GetMapping+PostMapping+PutMapping+DeleteMapping
// @RequestMapping不安全,不具备标识意义
//@compontent=repository+service+controller
}
注:方法如以上,hell1到hello5通过浏览器打印到控制台输出;hello6,hello7,hello8以及type1到type4用Apikit软件发送到打印台。
方法6中@requestBody需要导入依赖,上面pom.xml已经是完整版
hello6的打印方法,及结果
hello6的打印方法第二种写法
hello7及hello8的打印结果,此处不详细,可以去搜索资料看怎么熟练并应用Apikit
2.向页面传参
package com.zlj.web;
import com.zlj.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* @author zlj
* @create 2023-09-06 18:20
*/
@Controller
@RequestMapping("/rs")
public class ReturnController {
@RequestMapping("/hello1")
public void hello1(HttpServletResponse response){
Map<String,Object> map=new HashMap<>();
map.put("code",200);
map.put("msg","成功添加。。。");
try {
ResponseUtil.writeJson(response,map);
} catch (Exception e) {
e.printStackTrace();
}
}
@ResponseBody //响应json数据
@RequestMapping("/hello2")
public Map hello2(HttpServletResponse response){
Map<String,Object> map=new HashMap<>();
map.put("code",200);
map.put("msg","成功添加。。。");
return map;
}
@RequestMapping("/hello3")
public String hello3(){
return "index";
}
//String+mondel
@RequestMapping("/hello4")
public String hello4(Model model, HttpServletRequest request){
model.addAttribute("currenName","永州鸭");
request.setAttribute("location","道州");
return "index";
}
//modelandView
@RequestMapping("/hello5")
public ModelAndView hello5(){
ModelAndView mv=new ModelAndView();
mv.addObject("sign","肥而不腻");
mv.setViewName("index");
return mv;
}
}
<%--
Created by IntelliJ IDEA.
User: 朱
Date: 2023/9/6
Time: 12:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>springmvc 雷猴</h1>
用户名:${currenName}
原产地:${location}
评价:${sign}
</body>
</html>
3.页面跳转(hello6,hello7,hello8,hello9)
package com.zlj.web;
import com.zlj.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* @author zlj
* @create 2023-09-06 18:20
*/
@Controller
@RequestMapping("/rs")
public class ReturnController {
@RequestMapping("/hello1")
public void hello1(HttpServletResponse response) {
Map<String, Object> map = new HashMap<>();
map.put("code", 200);
map.put("msg", "成功添加。。。");
try {
ResponseUtil.writeJson(response, map);
} catch (Exception e) {
e.printStackTrace();
}
}
@ResponseBody //响应json数据
@RequestMapping("/hello2")
public Map hello2(HttpServletResponse response) {
Map<String, Object> map = new HashMap<>();
map.put("code", 200);
map.put("msg", "成功添加。。。");
return map;
}
@RequestMapping("/hello3")
public String hello3() {
return "index";
}
//String+mondel
@RequestMapping("/hello4")
public String hello4(Model model, HttpServletRequest request) {
model.addAttribute("currenName", "永州鸭");
request.setAttribute("location", "道州");
return "index";
}
//modelandView
@RequestMapping("/hello5")
public ModelAndView hello5() {
ModelAndView mv = new ModelAndView();
mv.addObject("sign", "肥而不腻");
mv.setViewName("index");
return mv;
}
// 场景一:转发到后台的某一个方法(当前类)
@RequestMapping("/hello6")
public String hello6() {
System.out.println("helo6...");
return "forward:hello2";
}
// 场景二:转发到后台的某一个方法(其他类)
@RequestMapping("/hello7")
public String hello7() {
System.out.println("hello7...");
return "forward:/param/hello1";
}
// 场景三:重定向到后台的某一个方法(当前类)
@RequestMapping("/hello8")
public String hello8() {
System.out.println("helo8...");
return "redirect:hello2";
}
// 场景四:重定向到后台的某一个方法(其他类)
@RequestMapping("/hello9")
public String hello9() {
System.out.println("hello9...");
return "redirect:/param/hello1";
}
}