目录
2.2 @RestController = @ResponseBody + @Controller
4. 请求转发(forward)或请求重定向(redirect)
1. 返回静态页面
直接在这里返回的,那么返回的就是静态页面的名称,比如
@RequestMapping("/resp")
@Controller
//@ResponseBody
public class RespController {
@RequestMapping("/Hi")
public String sayHi(){
return "/hello.html";
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.
0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>hello,spring mvc</title>
<script src="index.js"></script>
</head>
<body>
<h1>Hello,Spring MVC.</h1>
</body>
</html>
可以看到前面返回的内容是 hello.html,这里通过一级目录 sayhi,也可以访问到这个页面,说明返回的虽然是 String 类型,但实际上是一个页面
2. 返回非静态页面
2.1 @ResponseBody 返回页面内容
@Controller
@ResponseBody
public class TestController {
@RequestMapping("/Hi")
public String sayHi() {
return "hello.html";
}
}
2.2 @RestController = @ResponseBody + @Controller
@RequestMapping("/resp")
//@Controller
//@ResponseBody
@RestController
public class RespController {
@RequestMapping("/Hi")
public String sayHi(){
return "/hello.html";
}
}
2.3 示例:实现简单计算的功能
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.
0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>document</title>
<script src="index.js"></script>
</head>
<body>
<form action="/calc">
<div style="margin-top:100px;text-align: center;">
<h1>计算器</h1>
数字1:<input name="num1" type="text"><br>
数字2:<input name="num2" type="text"><br>
<input type="submit" value=" 点击相加 ">
</div>
</form>
</body>
</html>
@Controller
@ResponseBody
public class CalcController {
@RequestMapping("/calc")
public String calc(Integer num1,Integer num2){
if (num1 == num2 || num2 == null){
return "参数错误";
}
return "结果=" + (num1 + num2);
}
}
3. 返回JSON对象
@RequestMapping("/respjson")
public HashMap<String, String> respJson() {
HashMap<String, String> map = new HashMap<>();
map.put("Java", "Java Value");
map.put("MySQL", "MySQL Value");
map.put("Redis", "Redis Value");
return map;
}
3.1 实现登录功能,返回 JSON 对象
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.
0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="js/jquery-2.1.4.min.js"></script>
<title>Document</title>
<script>
// ajax 提交
function mysub() {
// 判空
// var username = $("#username");
//这里注意如果在js中有多个 jquery 那么这里的$,是不合适的,$相当于小名,jQuery相当于大名,最好使用大名
var username = jQuery("#username");
var password = jQuery("#password");
if(jQuery.trim(username.val()) == "") {
alert("请先输入用户名!");
username.focus; // 让光标重置到此元素
return;
}
if(jQuery.trim(password.val()) == "") {
alert("请先输入密码!");
password.focus();
return;
}
jQuery.ajax({
url:"/user/login2",
type:"POST",
data:{"username":username.val(),
"password":password.val()},
success:function(result) {
alert(JSON.stringify(result));
}
});
}
</script>
</head>
<body>
<div style="text-align: center;">
<h1>登录</h1>
⽤户:<input id="username">
<br>
密码:<input id="password" type="password">
<br>
<input type="button" value=" 提交 " onclick="mysub()" style="margin-top: 20px;margin-left: 50px;">
</div>
</body>
</html>
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login2")
public HashMap<String, Object> login2(String username, String password) {
HashMap<String, Object> result = new HashMap<>();
int state = 200;
int data = -1; // 等于1,登录成功,否则登录失败
String msg = "未知错误";
if(StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
if(username.equals("admin") && password.equals("admin")) {
data = 1;
msg = "";
} else {
msg = "用户名或密码错误!";
}
} else { //说明参数为空
msg = "非法参数";
}
result.put("state", state);
result.put("data", data);
result.put("msg",msg);
return result;
}
}
4. 请求转发(forward)或请求重定向(redirect)
4.1 请求转发:forward
@Controller
public class TestController {
//请求转发方式1
@RequestMapping("/fw")
public String myForward() {
return "forward:/hello.html";
}
//请求转发方式2
@RequestMapping("/fw2")
public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/hello.html").forward(request,response);
}
}
4.2 请求重定向:redirect
@Controller
public class TestController {
//请求重定向方式1
@RequestMapping("/rd")
public String myRedirect() {
return "redirect:/hello.html";
}
//请求重定向方式2
@RequestMapping("/rd2")
public void myRedirect2(HttpServletResponse response) throws IOException {
response.sendRedirect("/hello.html");
}
}