tonglin0325的个人主页

Spring MVC学习笔记——给Controller和视图传值

**  一、给Controller传值,值将显示在控制台**

  1.第一种:使用@RequestParam,改HelloController.java

1
2
3
4
5
6
7
8
//RequestMapping表示用哪一个url来对应
@RequestMapping({"/hello","/"})
public String hello(@RequestParam("username") String username){
System.out.println("hello");
System.out.println(username);
return "hello";
}

   然后在浏览器中输入请求

1
2
http://localhost:8080/SpringMVC_hello/hello?username=123

   控制台可以看到传的值

  但是使用了RequestParam,如果在请求中不传值的话,会报400错误,因为默认把参数作为了地址的一部分

**  2.第二种,把RequestParam删除,直接String username**

1
2
3
4
5
6
7
8
//RequestMapping表示用哪一个url来对应
@RequestMapping({"/hello","/"})
public String hello(String username){
System.out.println("hello");
System.out.println(username);
return "hello";
}

   这种可以不传值,不传值时候为null

**  二、再从Controller给视图传值**

**  1.用Map来传值**

**  修改HelloController.java文件,用Map<String,Object> context,不过不太建议用Map**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.common.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

//RequestMapping表示用哪一个url来对应,从Controller给视图传值
@RequestMapping({"/hello","/"})
public String hello(String username,Map<String,Object> context){
context.put("username", username); //从Controller给视图传值
System.out.println(username);
System.out.println("hello");
return "hello";
}

}

**  hello.jsp文件**

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello!! ${username}!!</h1>
</body>
</html>

 输入

1
2
http://localhost:8080/SpringMVC_hello/hello?username=123

 

 **  2.用Model来传值**

**  HelloController.java文件改为**

1
2
3
public String hello(String username,Model model){
model.addAttribute("String", username); //使用Model从Controller给视图传值

   或者addAttribute只有一个参数,这时是默认使用username的类型(String)作为参数

1
2
3
//等于model.addAttribute("String",username);
model.addAttribute(username);

 

  完整文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package org.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

//RequestMapping表示用哪一个url来对应
@RequestMapping({"/hello","/"})
public String hello(String username,Model model){
System.out.println("hello");
model.addAttribute("username", username);

//等于model.addAttribute("String",username);
model.addAttribute(username);
//model.addAttribute(new User());等于model.addAttribute("user",new User());
System.out.println(username);
return "hello";
}

@RequestMapping("/welcome")
public String welcome(){
System.out.println("welcome");
return "welcome";
}

}

   hello.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello!! ${username}!!</h1>
${string}
</body>
</html>

 

注解式控制器参考: