1.view--->controller
1)public String welcome(@RequestParam("username") String username) 必须传递username 不然会报错404
2)直接在函数参数列表中添加参数接收 public String welcome(String username) 不强制必须传递username
2.controller--->view
1)用一个map 例如:Map<String, Object>context ,然后向context中put进去要传的参数,context.put("username", username)
package com.join.spring.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String welcome(@RequestParam("username") String username,
Map<String, Object> context) {
context.put("username", username);
System.out.println(username);
return "welcome";
}
}
2)用Model来实现
package com.join.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String welcome(@RequestParam("username") String username,
Model model) {
model.addAttribute("username", username);
System.out.println(username);
return "welcome";
}
}
</pre><pre name="code" class="java">