帮同学改bug系列:(Spring是个好东西
如下是TestController的代码:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
@Controller
public class TestController {
@RequestMapping(value = "/book", method = RequestMethod.GET)
public String methodGet() {
System.out.println("get方法");
return "redirect:/index.jsp";
}
@RequestMapping(value = "/book", method =RequestMethod.POST)
public String methodPost() {
System.out.println("POST方法");
return "redirect:/index.jsp";
}
}
如下是jsp页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/book">get方法测试</a><br>
<hr>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="submit">
</form>
<hr>
</body>
</html>
理论情况下,点击 get方法测试 和 提交按钮时,控制台分别打印 get方法 和 post方法
但是出现如下bug
解决方案是:
在发送请求时,action后加上 / 即可
修改前:
修改后:
最后成功解决,如下图
原因分析:
不加斜杠就是在当前目录下查找方法,加斜杠就是在根目录下查找方法。
困扰了两天,终于解决了
Spring Boot 面试精讲