1.参数传递
4.0.0
org.example
ssm
1.0-SNAPSHOT
war
ssm Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
3.7.0
5.0.2.RELEASE
3.4.5
5.1.44
5.1.2
1.3.1
2.1.1
2.4.3
2.9.1
3.2.0
1.7.13
4.12
4.0.0
1.18.2
1.2
1.1.2
5.0.2.RELEASE
2.9.3
org.springframework
spring-context
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
mysql
mysql-connector-java
${mysql.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
org.apache.commons
commons-dbcp2
${commons.dbcp2.version}
org.apache.commons
commons-pool2
${commons.pool2.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
jcl-over-slf4j
${slf4j.version}
runtime
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.apache.logging.log4j
log4j-slf4j-impl
${log4j2.version}
org.apache.logging.log4j
log4j-web
${log4j2.version}
runtime
com.lmax
disruptor
${log4j2.disruptor.version}
junit
junit
${junit.version}
javax.servlet
javax.servlet-api
${servlet.version}
provided
org.projectlombok
lombok
${lombok.version}
provided
org.springframework
spring-webmvc
${spring.version}
jstl
jstl
${jstl.version}
taglibs
standard
${standard.version}
ssm
src/main/java
**/*.xml
src/main/resources
jdbc.properties
*.xml
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin.version}
${maven.compiler.target}
${project.build.sourceEncoding}
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
${mysql.version}
true
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
//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" %>
Title
springmvc 雷猴
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 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 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" %>
Title
springmvc 雷猴
用户名:${currenName}
原产地:${location}
评价:${sign}
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 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 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";
}
}